How to apply custom fonts on android with androidx

Viewed 144

I want to apply custom fonts on my android app which is using androidx (not support library!!)

But all of the ways for this that i found by searching are for support library.

They say that use android.support.v7.widget.appcompatTextview. But I have to use androidx.

I tried a lot of things

  1. using both
<item name="android:fontFamily">@font/custom_font_family</item>
<item name="fontFamily">@font/custom_pen_font_family</item>
  1. For applying all API level. custom_font_family.xml of /font
android:font="@font/nanumbarunpen"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/nanumbarunpen"
app:fontStyle="normal"
app:fontWeight="500" />
  1. set a value to attribute of the widget directly.
<TextView
android:fontFamily="@font/custom_font_family"
.../>

even those attempts, it was all ignored.

But! preview display custom font correctly! (still no changes in my real phone...)

help me....please,,,,....... TT

1 Answers

I didn't notice any difference between androidx and the support library before. You can add font folder under res, import your fonts and use them like below:

      <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/iransans_fanum"
        android:textColor="@color/white"
        android:textSize="25sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

Are you sure that you don't set any style programmatically ?

Related