18

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 ?

Navid Abutorab
  • 1,667
  • 7
  • 31
  • 58

4 Answers4

40

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>
Barryrowe
  • 1,313
  • 10
  • 16
4

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.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
2

Make sure all fonts should be available in font dir under res dir. Example structure below: enter image description here

And apply fonts whenever you want: enter image description here

vivek
  • 1,034
  • 11
  • 14
-1

On Real Device, try connecting to the internet. That solves.
[Considering you have done all necessary stuffs]

  • What's "all necessary stuffs"? You need to make your answer as clear as possible. Also should explain why " connecting to the internet" would solve this problem. – javdromero Sep 16 '21 at 18:38