-1

I have a root theme and an inheriting child theme:

<resources>
    <color name="colorRed">#FF0000</color>
    <color name="colorGreen">#00FF00</color>

    <style name="Root" parent="Theme.AppCompat.Light.DarkActionBar" />

    <style name="Root.TextAppearance" parent="android:TextAppearance">
        <item name="android:textColor">@color/colorGreen</item>
    </style>

    <style name="Child" parent="Root">
        <item name="android:textColor">@color/colorRed</item>
    </style>

</resources> 

In the manifest file I set the theme Child. In the layout I apply the text appearance from the root theme:

<TextView
    android:textAppearance="@style/Root.TextAppearance"
    android:text="Hello World!"
  • Expected: green text
  • Actual: red text

How does the Root.TextAppearance inherit the color red?

Blcknx
  • 1,921
  • 1
  • 12
  • 38

3 Answers3

2

You should set the style not the textAppearance

style="@style/Root.TextAppearance"

also in styles Root.TextAppearance should have Root as parent

<style Root.TextAppearance parent="android:TextAppearance">

than set android:textAppearance="@style/Root.TextAppearance" and it should work

TibiG
  • 819
  • 1
  • 6
  • 18
  • Fact! It works and only works, when I use style. It's a pity I can't use textAppearance to modify the style. – Blcknx May 24 '18 at 12:12
  • Give me some time to chose the best of all incoming answers. This one is really useful, but not fully satisfying. I crashes my concept of theming, that strongly depends on the option to modify the style by textAppearance. – Blcknx May 24 '18 at 12:17
  • @Blcknx I've edited the answer. It didn't work because your TextAppearance style didn't have a parent. So it wasn't able to inherit the textColor attribute from it. – TibiG May 24 '18 at 13:14
  • Thank you @TibiG. I tried. It does not change the result. I still think it is a bug. It does not inherit the textColor attribute, as I overwrite it. It does inherit five other attributes, I don't make use of. – Blcknx May 24 '18 at 13:27
  • @TibiG I think I found out the reason for this behaviour, if it is not a bug. See my answer. What do you think. – Blcknx May 24 '18 at 17:49
  • 1
    @Blcknx seems legit :) – TibiG May 25 '18 at 07:50
1

Checking the order of precedences

The short answer to the question is, that themes have precedence over android:textAppearance.

There are different types of hierarchies for Android. When using the styles attribute the styles hierarchy applies as expected. Assuming that the styles hierarchy also applies for the android:textAppearance attribute obviously fails.

There is another hierarchy for themes. This hierarchy follows down the layout tree. The theme Child is applied in the manifest and is the top level.

It seems the settings coming in by this top level theme even overrule the settings of android:textAppearance on a lower level. This still feels wrong, as lower levels usually should overwrite higher levels.

So I do some tests by applying the attributes style, android:theme and android:textAppearannce to find out the order of their strengths.

It turns out that texAppearance is the weakest and style is the strongest:

  1. style
  2. android:theme
  3. android:textAppearance

I think this the explanation why the theme from the higher level could overwrite the android:textAppearance on the lower level. I admit that I find the low precedence of textAppearance rather confusing. It's not how I expected it to work. However, that's the results I found out be testing.

Blcknx
  • 1,921
  • 1
  • 12
  • 38
  • precedence order mentioned here as well: https://medium.com/androiddevelopers/whats-your-text-s-appearance-f3a1729192d – AA_PV Jul 01 '20 at 18:27
0

make some change put color code into color.xml

    <color name="colorRed">#FF0000</color>
<color name="colorGreen">#00FF00</color>

then after try this..

        android:textAppearance="@style/Root.TextAppearance"
  • Thank you, but that's not the reason. I put in un purpose into one file, to keep the test case most simple. – Blcknx May 24 '18 at 12:29