Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - fontFamily not working on androidx

It's very strange that why fontFamily is not working on androidX .

this is my code:

   <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="testing "
        android:textSize="17sp"
        android:fontFamily="@font/iransans"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

the above code doesn't effect font at all , when I convert the textview to app compact, then it works :

<androidx.appcompat.widget.AppCompatTextView

i wanted to set my whole application font but it doesn't work as I expected :

<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:fontFamily">@font/iransans</item>
</style>

is there any way to solve this ? How can I use my font in entire app without setting font family for each view ?

like image 913
Navid Abutorab Avatar asked Oct 28 '19 14:10

Navid Abutorab


2 Answers

You are potentially running into 1+ of 3 things:

  1. You're setting android:fontFamily but not setting fontFamily in your theme. make sure you have both:
<item name="fontFamily">@font/iransans</item>
<item name="android:fontFamily">@font/iransans</item>
  1. If you're running on older API levels, you could be running into this bug, and an upgrade to the androidx.appcompat dependency might do the trick:

    https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha02

  2. If you have any other themes defined in your styles.xml, and the parent of your TextView is using a separate theme, that theme may be taking precedence over the fontFamily attribute on the view (I have not yet determined why, but this is the scenario I'm running into myself). Setting the android:theme directly on my TextView to a style that defines the font family did the trick.

<TextView
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:theme="@style/MyCustomFontTextAppearance" />

in styles.xml:

<style name="MyCustomFontTextAppearance" parent="TextAppearance.AppCompat.Body1">
    <item name="fontFamily">@font/iransans</item>
    <item name="android:fontFamily">@font/iransans</item>
</style>
like image 183
Barryrowe Avatar answered Nov 13 '22 14:11

Barryrowe


I had the similar issue, when my layout looked correctly (with custom fonts applied) in IDE preview, but without fonts on real device.

The reason of the issue was that my activity was inherited from Activity, not AppCompatActivity. Changing to AppCompatActivity resolved the issue without renaming all TextViews to AppCompatTextViews.

like image 3
Vasily Kabunov Avatar answered Nov 13 '22 15:11

Vasily Kabunov