Custom font is working on XML preview but not working on RecyclerView in Android

Viewed 593

Code for TextView with custom font declared on style="@style/SbTextView.ListTitle"

<TextView
    android:id="@+id/symbolCompany"
    style="@style/SbTextView.ListTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:typeface="normal"
    tools:ignore="MissingPrefix"
    tools:text="@sample/watchlist_companies.json/data/symbol" />

Style using custom font @font/sb_proxima with BOLD textStyle:

<style name="SbTextView.ListTitle" parent="SbTextView">
    <item name="android:fontFamily">@font/sb_proxima</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">@dimen/text_body</item>
    <item name="android:textColor">@color/contentcolor</item>
    <item name="android:lineSpacingMultiplier">1.15</item>
</style>

Style parent SbTextView

<style name="SbTextView" parent="@android:style/TextAppearance"/>

Font family @font/sb_proxima

<?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/proxima_nova_regular"
        android:fontStyle="normal"
        android:fontWeight="400"

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

    <font
        android:font="@font/proxima_nova_regular"
        android:fontStyle="italic"
        android:fontWeight="400"

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

</font-family>

What rendered on design preview (on XML) look like the following picture

XML Preview

XML Preview

On runtime inside RecyclerView look like rendered on RecylerView

Rendered on RecylerView

1 Answers

in your @font/sb_proxima file, you need to define bold font like below:

<?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/proxima_nova_regular"
    android:fontStyle="normal"
    android:fontWeight="400"

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

<font
    android:font="@font/proxima_nova_regular"
    android:fontStyle="italic"
    android:fontWeight="400"

    app:font="@font/proxima_nova_regular"
    app:fontStyle="normal"
    app:fontWeight="400"/>
<font
    android:font="@font/proxima_nova_bold"
    android:fontStyle="bold"
    android:fontWeight="400"

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

</font-family>

Also, don't forget to include proxima_nova_bold file.

EDIT

In the documentation they mentioned:

android:fontWeight Integer. The weight of the font. This attribute is used when the font is loaded into the font stack and overrides any weight information in the font's header tables. The attribute value must be a positive number, a multiple of 100, and between 100 and 900, inclusive. If you do not specify the attribute, the app uses the value from the font's header tables.The most common values are 400 for regular weight and 700 for bold weight.

In your font file you have mentioned font weight as 400 which is commonly used for regular. Try with 700 font weight.

Related