Skip to content Skip to sidebar Skip to footer

Android - How To Specify Special Attribute To Some Specific View In Different Theme

I am working on a 'dynamic change theme' function of my app (light and dark). I have some special customized widget which is using special color. And I also want to specify the col

Solution 1:

First, define two colors in /res/values/colors.xml:

<resources><!-- ActionBar--><colorname="MySpecialTextColorLight">@color/white</color><colorname="MySpecialTextColorDark">@color/black</color><resources>

Then define an attribute with a unique name in /res/values/attrs.xml:

<resources><attrname="mytheme_text_color"format="color"/></resources>

Next, define value for this attribute in your two themes, like:

<resources><stylename="MyTheme"parent="android:Theme"><itemname="mytheme_text_color">@color/MySpecialTextColorDark</item></style><stylename="MyTheme.Light"parent="android:Theme.Light"><itemname="mytheme_text_color">@color/MySpecialTextColorLight</item></style></resources>

Finally, use the reference attribute you define to refer to these colors. In your case, you use:

<TextView...android:color="?attr/mytheme_text_color".../>

Post a Comment for "Android - How To Specify Special Attribute To Some Specific View In Different Theme"