Richard
Richard

Reputation: 7433

Font family resource from theme not applied to TextViews

I want to set the android:textAppearance of several TextViews. To do that, I have declared the following style:

<style name="TextAppearance.MyTheme.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
    <item name="android:fontFamily">@font/montserrat</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textFontWeight">400</item>
    <item name="android:textSize">@dimen/text_size_215</item>
</style>

Said style will be set to the attr of textAppearanceHeadline in my theme like so:

<item name="textAppearanceHeadline1">@style/TextAppearance.MyTheme.Headline1</item>

The font family XML is written like so:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <font
        app:fontStyle="normal"
        app:fontWeight="200"
        app:font="@font/montserrat_extralight" />

    <font
        app:fontStyle="normal"
        app:fontWeight="400"
        app:font="@font/montserrat_regular" />

    <font
        app:fontStyle="normal"
        app:fontWeight="500"
        app:font="@font/montserrat_medium" />

    <font
        app:fontStyle="normal"
        app:fontWeight="600"
        app:font="@font/montserrat_semibold" />
</font-family>

All the @font/montserrat_* references are .ttf files that I've included in my resources folder. Then, I will set the style using said attr value to the TextView I want to style like so:

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Writing Words"
    android:textAppearance="?attr/textAppearanceHeadline1"
    app:layout_constraintEnd_toEndOf="@+id/headingView"
    app:layout_constraintStart_toStartOf="@+id/headingView"
    app:layout_constraintTop_toBottomOf="@+id/headingView" />

The problem is, the font family and font-weight are applied incorrectly (they change to the default font family and font-weight). The size is applied correctly. Did I do something wrong here?

UPDATE

When I try setting only either android:textAppearance or style to the TextView with ?attr/textAppearanceHeadline1, it doesn't set the font-family and font-weight. However, when I set android:fontFamily and android:fontWeight alongside android:textAppearance, it works. Setting those alongside style does not work. Why is that so? What's happening?

Upvotes: 1

Views: 1456

Answers (2)

Richard
Richard

Reputation: 7433

Apparently, I have to override fontFamily also. Overriding fontFamily in my style makes it work flawlessly. The style becomes the following.

<style name="TextAppearance.MyTheme.Headline1" parent="TextAppearance.MaterialComponents.Headline1">
    <item name="fontFamily">@font/montserrat</item>
    <item name="android:fontFamily">@font/montserrat</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textFontWeight">400</item>
    <item name="android:textSize">@dimen/text_size_215</item>
</style>

Upvotes: 3

i30mb1
i30mb1

Reputation: 4776

Probably in your xml exist viewGroup (where your's TextView one of the child) with style that have android:fontFamily that override your's android:textAppearance

Upvotes: -1

Related Questions