Android - style override for each theme

Is there a way to change the style based on which theme is set for activity? for example, suppose my activity has a text view with a style set to TextViewStyle

<TextView style="@style/TextViewStyle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Text"/> 

This is my TextViewStyle

 <style name="TextViewStyle" parent="android:style/Widget.TextView"> <item name="android:textStyle">bold</item> <item name="android:textSize">8sp</item> <item name="android:textColor">@android:color/holo_blue_dark</item> </style> 

In my Styles.xml I have two themes

  <style name="AppThemeLight" parent="android:Theme.Material.Light"> </style> <style name="AppThemeDark" parent="android:Theme.Material"> </style> 

Now that my theme is set to Dark, I want this theme to override some of the values ​​set in my TextViewStyle. How can i do this? I tried the following but not working

  <!-- Override TextViewStyle style for AppThemeLight--> <style name ="AppThemeLight.TextBlockStyle" parent="@style/TextBlockStyle"> <item name="android:textColor">@color/my_purple</item> <item name="android:textSize">20sp</item> </style> <!-- Override TextViewStyle style for AppThemeDark--> <style name ="AppThemeDark.TextBlockStyle" parent="@style/TextBlockStyle"> <item name="android:textColor">@color/my_green</item> <item name="android:textSize">40sp</item> </style> 
+5
source share
3 answers

Well, that’s how I achieved this, 1 hour after posting the question! May be useful for someone else!

At first in attrs.xml I defined the following:

  <attr name="textBlockStyle" format="reference"/> 

In my .xml style

 <style name="TextBlockStyle" parent="android:style/Widget.TextView"> <item name="android:textStyle">bold</item> <item name="android:textSize">8sp</item> <item name="android:textColor">@android:color/holo_blue_dark</item> </style> <style name="AppThemeLight" parent="android:Theme.Material.Light"> <item name="android:windowNoTitle">true</item> <item name="textBlockStyle">@style/AppThemeLightTextBlockStyle</item> </style> <style name="AppThemeDark" parent="android:Theme.Material"> <item name="android:windowNoTitle">true</item> <item name="textBlockStyle">@style/AppThemeDarkTextBlockStyle</item> </style> <!-- Override TextBlockStyle style for AppThemeLight --> <style name ="AppThemeLightTextBlockStyle" parent="@style/TextBlockStyle"> <item name="android:textColor">@color/my_purple</item> <item name="android:textSize">20sp</item> </style> <!-- Override MyButton style for AppThemeDark --> <style name ="AppThemeDarkTextBlockStyle" parent="@style/TextBlockStyle"> <item name="android:textColor">@color/my_green</item> <item name="android:textSize">40sp</item> </style> 

Notice how I use the attribute that I defined to reference the style

 <item name="textBlockStyle">@style/AppThemeLightTextBlockStyle</item> 

Finally, on my element, I set a style similar to this: style = "textBlockStyle":

 <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" style="?textBlockStyle" android:text="THIS IS SOME STLE...." android:gravity="center" /> 

When I set the activity theme to dark or light, the correct textButtenStyle will be used

+11
source

Add a textStyle element to your activity style and make it equal to the desired style, and then when the activity is loaded, all text elements inside will have the style specified inside its theme, unless you redefine it in text format

0
source

Try something like

 <item name="android:textAppearance">@style/yourTextViewStyle</item> 

Inside the style of your activity

0
source

All Articles