Tsunaze
Tsunaze

Reputation: 3254

text appearance doesn't work inside my theme

I'm trying to put a style in all my app, so i created a theme with my style inside :

<resources>
    <style name="MyTheme" parent="android:Theme.Holo.Light">
        <item name="android:textAppearance">@style/subtitle</item>
    </style>

    <style name="subtitle parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/purple</item>
        <item name="android:textSize">40sp</item>
    </style>
</resources>

But textAppearance doesn't work it stay the same, but when i put something like textColor in my theme, it works

Upvotes: 3

Views: 2308

Answers (4)

This is a quite old question, but the answer may help someone. The key to solving this is in the "precedence order of styling techniques" here:  precedenceOrderOfDifferentStylingTechniquesImage

on the top is the highest precedence, at the bottom is the lowest precedence.

As we can see, theme has the lowest precedence. In your example, your android:textAppearance property is being overridden by the default style of every view that accepts this attribute.

The default style property is defined in every them for every specific view that accepts this attribute: in this case android:Theme.Holo.Light provides the default style for textView as android:textViewStyle... for buttons is android:buttonStyle (which inherits its textAppearance from TextView), and so on.

So if you are trying to apply that android:textAppearance property to a TextVew you should use <item name="android:textViewStyle">@style/subtitle</item> instead of <item name="android:textAppearance">@style/subtitle</item> inside MyTheme. A way to verify this is setting android:textViewStyle to null, that way your current code will work fine with textViews <item name="android:textViewStyle">null</item>

This post explains this precedence a bit deeper:

https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d

Upvotes: 10

vida
vida

Reputation: 4499

For TextView, try android:textAppearanceSmall inside your theme instead.

Upvotes: 0

RainClick
RainClick

Reputation: 101

Depends on your target API you need to put your customization code in different /res/values-vxx/style.xml files.

Upvotes: 0

Usama Sarwar
Usama Sarwar

Reputation: 9020

What I can see is, you have not declared the color in your xml for theme. Please add the following line within the <resources> and try. Your xml will look like:

<resources>
<style name="MyTheme" parent="android:Theme.Holo.Light">
    <item name="android:textAppearance">@style/subtitle</item>
</style>

 <color name="purple">code for your color</color>

<style name="subtitle parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/purple</item>
    <item name="android:textSize">40sp</item>
</style>

I think this will do.

Upvotes: 0

Related Questions