'Downloadable Font's font weight not working android

I was following this official guide and wanted to use Poppins font for my project. The font has changed but the font weight property does not seem to be work.

I have a style defined as follows:

<style name="TextAppearance.SectionTitle" parent="TextAppearance.MaterialComponents.Headline4">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">#223263</item>
    <item name="android:fontWeight">700</item>
    <item name="fontWeight">700</item>
</style>

And I'm applying it as follows:

<TextView
    ...
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/category"
    android:textStyle="bold"
    android:textFontWeight="700"
    style="@style/TextAppearance.SectionTitle"
/>

As you can see, I have tried it in several ways but none have been successful yet. The color of the font and size changes according to the style I have defined but the bold effect is not registered.

This is the output I get:

enter image description here

Expected output:

enter image description here

My poppins.xml:

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
    app:fontProviderAuthority="com.google.android.gms.fonts"
    app:fontProviderPackage="com.google.android.gms"
    app:fontProviderQuery="Poppins"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

preloaded_fonts.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <array name="preloaded_fonts" translatable="false">
    <item>@font/poppins</item>
    <item>@font/poppins_bold</item>
  </array>
</resources>


Solution 1:[1]

I'm not sure if this is the proper fix but I managed to solve it by replacing the font weight with font family inside my style tag as follows:

<style name="TextAppearance.SectionTitle" parent="TextAppearance.MaterialComponents.Headline4">
    ...
    <item name="android:fontFamily">@font/poppins_bold</item>
    <item name="fontFamily">@font/poppins_bold</item>
</style>

You need to add the bold font of your selected font in your resources for this:

Adding bold version of the font

Solution 2:[2]

Have you tried this answer? We have implemented it with Inter Google Font and it worked well, displaying the right weights.

res/font/inter.xml

<?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
        android:font="@font/inter_thin"
        android:fontStyle="normal"
        android:fontWeight="100"
        app:font="@font/inter_thin"
        app:fontStyle="normal"
        app:fontWeight="100" />

    <font
        android:font="@font/inter_extralight"
        android:fontStyle="normal"
        android:fontWeight="200"
        app:font="@font/inter_extralight"
        app:fontStyle="normal"
        app:fontWeight="200" />

    <font
        android:font="@font/inter_light"
        android:fontStyle="normal"
        android:fontWeight="300"
        app:font="@font/inter_light"
        app:fontStyle="normal"
        app:fontWeight="300" />

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

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

    <font
        android:font="@font/inter_semibold"
        android:fontStyle="normal"
        android:fontWeight="600"
        app:font="@font/inter_semibold"
        app:fontStyle="normal"
        app:fontWeight="600" />

    <font
        android:font="@font/inter_bold"
        android:fontStyle="normal"
        android:fontWeight="700"
        app:font="@font/inter_bold"
        app:fontStyle="normal"
        app:fontWeight="700" />

    <font
        android:font="@font/inter_extrabold"
        android:fontStyle="normal"
        android:fontWeight="800"
        app:font="@font/inter_extrabold"
        app:fontStyle="normal"
        app:fontWeight="800" />

    <font
        android:font="@font/inter_black"
        android:fontStyle="normal"
        android:fontWeight="900"
        app:font="@font/inter_black"
        app:fontStyle="normal"
        app:fontWeight="900" />

</font-family>

All ttf files for Inter font are placed in res/font aswell.

This is an example of how we apply it to TextView styles:

res/values/styles.xml

    <style name="TextAppearance.Medium">
        <item name="android:fontFamily">@font/inter</item>
    </style>

or

    <style name="Whatever">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:fontFamily">@font/inter</item>
    </style>

I hope the examples are helpful for someone else

Solution 3:[3]

I ran into the same thing--I'm not sure textFontWeight works correctly with downloadable fonts. Maybe it doesn't prompt the app to actually download the font for that weight?

You can fix it by creating a font resource for each weight you want to use. Try adding this font resource:

poppins_bold.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
    app:fontProviderAuthority="com.google.android.gms.fonts"
    app:fontProviderPackage="com.google.android.gms"
    app:fontProviderQuery="name=Poppins&amp;weight=700"
    app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>

You may also have to add an entry to res/layout/values/preloaded_fonts.xml:

<item>@font/poppins_bold</item>

Then reference it with android:fontFamily="@font/poppins_bold.

FWIW, the same changes will be generated if you add the font via selecting the weight in the Layout Editor, but I find it easier to just duplicate the font resource and edit the fontProviderQuery.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rubek Joshi
Solution 2 voghDev
Solution 3 Christopher Pickslay