android - fontFamily not working on androidx
Asked Answered
N

4

19

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 ?

Novah answered 28/10, 2019 at 14:39 Comment(4)
what do you mean by not working? Is it showing an error? please specifyKrawczyk
is the font file present in your project ?Krawczyk
@RohitRawat no I don't have any errors but the font is not effecting at allNovah
@RohitRawat yes , the font exist in the font resources directoryNovah
U
42

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>
Understandable answered 23/1, 2020 at 20:30 Comment(5)
Number 3 was my issue! I was using android:textAppearance instead of android:themeExtravasate
this should be marked as the right answer, imoTome
the third option solved my issue, but the question is that why setting style on textview cannot override the default fontfamily defined in the app theme and I should manually set both theme and textAppearanceProportionable
+ number four is inheriting from AppCompatActivity ;) And thanks for explanationSherborne
Thanks! In my case <item name="fontFamily"> contained an old font. I have searched for this solution for many hours.Lug
L
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.

Lavonia answered 24/8, 2021 at 9:20 Comment(0)
H
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

Hexagon answered 9/11, 2021 at 17:1 Comment(0)
I
-2

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

Ipomoea answered 16/9, 2021 at 9:53 Comment(2)
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.Fawkes
Necessary stuffs: 1. Add Font File: Place your custom font file (e.g., custom_font.ttf) in the res/font directory. 2. Update XML Layout: Add android:fontFamily="@font/custom_font" to your TextView in the XML layout. And try referencing @Barryrowe's answer as well.Ipomoea

© 2022 - 2024 — McMap. All rights reserved.