0

I have a trivial Android app and fail to understand, why my global style definition does not get applied to view elements.

This is my file res/values/styles.xml:

<resources>
    <style name="Theme.XXXXX" parent="Theme.MaterialComponents">
        <item name="android:textAppearance">@style/Theme.XXXXX.TextAppearance</item>
    </style>
    <style name="Theme.XXXXX.TextAppearance" parent="TextAppearance.MaterialComponents.Body1">
        <item name="android:textSize">24sp</item>
    </style>
</resources>

This is where I globally set the style in my manifest:

<application
    android:dataExtractionRules="@xml/data_extraction_rules"
    android:fullBackupContent="@xml/backup_rules"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.XXXXX">

This is a trivial fragment layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/dialog_padding">

    <TextView
        android:id="@+id/introduction_overview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/introduction_overview" />

    <LinearLayout
        android:id="@+id/introduction_usage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

My expectation is that the TextView "@+id/introduction_overview" inherits the font size defined in the styles definition. That however is not the case. Why not?

As a test I added the explicit style android:textAppearance="@style/Theme.XXXXX.TextAppearance" to the TextView. That works, the font size does get applied. But I do not want to set individual style for all my view elements! That is what a global style definition is for!

I fail to see where I might overwrite my own style definition. Or any explicit application of another style definition. This is a trivial app.

Does anyone have an idea what I might check, what I might have to change?

UPDATE:

As @CampNerd pointed out in the answer below this apparently is not possible any more in Android (though it clearly has been possible some 8-10 years ago). I fail to see the logic behind this limitation.

Why do I have to implement <item name="android:textAppearance">@style/Theme.XXXXX.TextAppearance</item> </style> if the line apparently is completely without effect inside the theme anyway? Since I have to refer to the referred style anyway in each and every view I implement later?

This does not make any sense at all to me.

arkascha
  • 41,620
  • 7
  • 58
  • 90

1 Answers1

0

What you did is correct. However all you did was set a style. In order for that style to be applied you have to point it somewhere. Example:

<TextView
    android:id="@+id/introduction_overview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/introduction_overview" 

style = "@style/theme.xxxxx.TextAppearnce"/>

This is how I always done it.

This may help text style

Camp Nerd
  • 327
  • 1
  • 11
  • That is exactly what I do _not_ want to do. As I wrote in the question. I do not want to refer to an individual style in each and every view in my layouts. – arkascha Apr 23 '23 at 12:14
  • @arkascha according to Android documentation, that's the only way you can do it. There is no global style. The style you make has to be explicit to the textview as You do not want to do.. you will have to make the style then reference it to the textview. That's Android official documentation. – Camp Nerd Apr 23 '23 at 13:21
  • Added documentation... https://developer.android.com/develop/ui/views/theming/themes#PlatformStyles – Camp Nerd Apr 23 '23 at 13:33
  • I begin to trust that you are right. I am just really surprised by this. I have little experience in Android, come from a web background and I think it is strange how primitive and cumbersome the Android style logic is compared to that. – arkascha Apr 23 '23 at 13:40
  • One more question about the _how_ to refer to that style: you use `style = "@style/theme.xxxxx.TextAppearnce"`, in the mean time learned that `android:textAppearance="?android:attr/textAppearance"` also works very well. Which variant would you prefer and why? – arkascha Apr 23 '23 at 13:41
  • Actually I found various old tutorials and SO answers that demonstrate exactly what I asked. But that apparently does not work any more. So styling capability in Android even degraded over time? – arkascha Apr 23 '23 at 13:43
  • You can use both.. it's just a developer preference – Camp Nerd Apr 23 '23 at 13:45
  • I will show an example in a few – Camp Nerd Apr 23 '23 at 13:46
  • This is completely frustrating. That means one has to manually set a style for each and every view element. That is a broken style system IMHO. Completely idiotic. Not pointing fingers here, just stating the technical implications. Endless code lines for nothing. An immense redundancy. And predictable inconsistencies of course. – arkascha Apr 23 '23 at 13:47
  • Yup... Now if you was using java I think you can do what your wanting. But kotlin has completely destroyed the concepts.. however, kotlin is the preferred language – Camp Nerd Apr 23 '23 at 14:13