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