Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

background issue with styles and themes

in attrs I have

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

then in theme

<style name="mytheme" parent="android:Theme">
    <item name="bzz">@color/aaa</item>
</style>

and in the code this works great

tv.setBackgroundResource(R.color.aaa);

but when I do this it gives me an error

tv.setBackgroundResource(R.attr.bzz);

I do not understand what is the problem, my logic is that I set the bzz as reference to color so that should work fine, but it does not :) it says like android.content.res.Resources$NotFoundException: Resource ID #0x7f010008 but I do not understand what resource can't be found ?

I am sure that the color is there sins if I set it directly it works great, what exacly is the thing that is not linked correctly

Thanks

like image 753
Lukap Avatar asked Dec 22 '22 07:12

Lukap


1 Answers

You need to resolve the attr to get the corresponding color's resource id. Then you can set the TextView's background resource to the obtained resource id.

Example code:

TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.bzz, typedValue, true);
tv.setBackgroundResource(typedValue.resourceId);
like image 187
Tiago Avatar answered Dec 28 '22 09:12

Tiago