I want to use colors from a Theme to apply it to some HTML my app is rendering. I am wondering if I can do that?
I am looking to use colors like they are specified in themes.xml:
<item name="colorBackground">@android:color/background_dark</item>
<item name="textColorPrimary">@android:color/primary_text_dark</item>
So looking at them they are declared in the same way. So I thought I could access them in the same way too.
That is not the cause though. When trying to access those values this way:
TypedValue tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.colorBackground, tv, true);
System.out.println("tv.string=" + tv.string);
System.out.println("tv.coerced=" + tv.coerceToString());
int colorResourceId = getResources().getColor(tv.resourceId);
System.out.println("colorResourceId=" + colorResourceId);
tv = new TypedValue();
getTheme().resolveAttribute(android.R.attr.textColorPrimary, tv, true);
System.out.println("tv.string=" + tv.string);
System.out.println("tv.coerced=" + tv.coerceToString());
colorResourceId = getResources().getColor(tv.resourceId);
System.out.println("colorResourceId=" + colorResourceId);
I get this as a result:
I/System.out( 1578): tv.string=null
I/System.out( 1578): tv.coerced=#ffffffff
I/System.out( 1578): colorResourceId=-1
I/System.out( 1578): tv.string=res/color/primary_text_light.xml
I/System.out( 1578): tv.coerced=res/color/primary_text_light.xml
I/System.out( 1578): colorResourceId=-16777216
The results are different. The first one actually gives me the color "#fffffff" which would work for me, the second one only gives me an xml.
Do I need to jump through a few more hoops here to resolve the actual color? Does my original intention work at all? Maybe it won't work, because colors could be arbitrary drawables?
I didn't find any relevant documentation, but if you know any, just point me there please.
Btw. I also tried obtainStyledAttributes(), but this had basically the same issues.