Global style rule in android does not get applied

163 Views Asked by At

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.

1

There are 1 best solutions below

10
Camp Nerd On

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