79376741

Date: 2025-01-22 06:47:19
Score: 0.5
Natty:
Report link

Very similar to your own suggestion I did it as follow:

private fun loadColor(intArray: Int): Int{
    val typedValue = TypedValue()
    context?.theme?.resolveAttribute(intArray, typedValue, true)
    return typedValue.data
}

Then, in my theme file I have:

<style name="Theme.MyAppName" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="boxColor2OnPrimary">@color/new_grey</item>
</style>

@color/new_grey is a color set in the colors.xml file

I then added to my attrs.xml file the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomAttributes">

        <attr name="boxColor2OnPrimary" format="color" />

    </declare-styleable>
</resources>

and finally when I want to get a color I simply do this:

val color = loadColor(boxColor2OnPrimary)

now you can make full use of your day / night theme

Reasons:
  • RegEx Blacklisted phrase (1): I want
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Assay