android - Theme a single TextView background color in styles.xml -


i have application has multiple themes. have single textview who's background needs change color each theme, other textview's stay default theme. created custom textview widget , set textview in xml layout file.

 public class customheadertextview extends textview {      public customheadertextview(context context) {         super(context);     }      public customheadertextview(context context, attributeset attrs)     {         super(context, attrs);     }      public customheadertextview(context context, attributeset attrs, int defstyleattr) {         super(context, attrs, defstyleattr);     } } 

layout

    <*my package*.customheadertextview         android:layout_width="250dp"         android:layout_height="40dp" /> 

how access custom textview , change background color within each of themes in styles.xml?

    <style name="apptheme.blue" parent="theme.appcompat.noactionbar">         <item name="colorprimary">@color/primarycolor_blue</item>         <item name="colorprimarydark">@color/primarycolordark_blue</item>         <item name="coloraccent">@color/primaryaccent_blue</item>          // set here         <item name="customheadertextview:backgroundcolor">@color/primarycolordark_blue</item>      </style>      <style name="apptheme.red" parent="theme.appcompat.light.noactionbar">         <item name="colorprimary">@color/primarycolor_red</item>         <item name="colorprimarydark">@color/primarycolordark_red</item>         <item name="coloraccent">@color/primaryaccent_red</item>          // set here         <item name="customheadertextview:backgroundcolor">@color/primarycolordark_red</item>     </style> 

i found way can set different background color specific textview. also, able set according each theme have.

solution:

creating own attribute custom attribute in attr.xml

below implementation:

step 1

first, create attr.xml file @ res/values folder , insert following content:

res/values/attr.xml

<?xml version="1.0" encoding="utf-8"?> <resources>     <attr name="customtextviewbackattributecolor" format="color" /> </resources> 

step 2

that attribute created should set color in every theme have below:

styles.xml

<resources>     <style name="apptheme.blue" parent="theme.appcompat.noactionbar">         <!-- specific text view color -->         <item name="customtextviewbackattributecolor">@color/color_for_theme_blue</item>     </style>      <style name="apptheme.red" parent="theme.appcompat.light.noactionbar">         <!-- specific text view color -->         <item name="customtextviewbackattributecolor">@color/color_for_theme_red</item>     </style> </resources> 

step 3

finally, set attribute background color of custom view.

note

you can set color background of specific textview. way, textview have different background color (and not default background color defined in each theme). way, don't need create customview have different background color.

res/layout/activity_layout.xml

<com.pivoto.gui.generic.customheadertextview     android:layout_width="250dp"     android:layout_height="40dp"     android:text="hello world!"     android:background="?customtextviewbackattributecolor"/>  <textview     android:layout_width="250dp"     android:layout_height="40dp"     android:text="hello world2!"     android:background="?customtextviewbackattributecolor"/>  <textview     android:layout_width="250dp"     android:layout_height="40dp"     android:text="hello world3!"     android:background="?customtextviewbackattributecolor"/> 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -