Is there a way to change a style based on what theme is is set on an activity? for example, suppose my activity has a textview 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, when I have my theme set to Dark, I want this theme to override some values I set in my TextViewStyle. How could I do this? I tried the following, but It does not work
<!-- 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>
Since android:theme is read and applied by the view inflater, which doesn’t have access to an arbitrary view’s default style attribute, android:theme is ignored when specified in a resource that’s used as as default style.
Theme overlays are used to override the theme attributes and you can even use them for views in a given view hierarchy. Text appearances can help you to shape your TextViews text-related attributes with android:textAppearance, while leaving style attribute free.
An attribute can be defined for a view as well as a theme. For instance, android:layout_width attribute is a view attribute whereas colorPrimary is a theme attribute. A view attribute is set in the view’s XML either by setting directly on the view tag or indirectly by using style (will be mentioned later).
Overriding Button Styles in Android Posted: 8/22/2016 Tagged under: androidbeginner-friendly Android comes with a ton of widgets, gizmos, and gadgets (maybe just widgets) out of the box, but it can be difficult to find one that works exactly how you would expect it to.
Ok here is how I achieved this, 1 hour after posting the question! Might be helpful for someone else!
First in attrs.xml I defined the following :
<attr name="textBlockStyle" format="reference"/>
Then in the style.xml
<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 I defined to reference a style
<item name="textBlockStyle">@style/AppThemeLightTextBlockStyle</item>
Finally, on my element I set the style like this "style="?textBlockStyle" :
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="?textBlockStyle"
android:text="...."
android:gravity="center" />
When I set my activity theme to Dark or Light, the correct textButtenStyle will be used
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With