Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get font from resources has bug in Java - Android

I write code and use CompatResources to get font and set as TypeFace to views bug Fabric.io reported some crashes about it .

Caused by android.content.res.Resources$NotFoundException Font resource ID #0x7f090000 could not be retrieved.

and it is my code :

tfFontIcon = ResourcesCompat.getFont(mContext, R.font.font_icon);

thanks for your help.

like image 935
Alireza Nazari Avatar asked Nov 07 '22 10:11

Alireza Nazari


1 Answers

Try below soultion

Solution 1

Add your font in assets folders like below image

enter image description here

And use below method to set font typeFace

CommonUtils.setFont(context, binding.txtDigital, "Montserrat-Bold.ttf");

public static void setFont(Context context, TextView textView, String fontPath) {
    Typeface t = Typeface.createFromAsset(context.getResources().getAssets(), fontPath);
    textView.setTypeface(t);
}

Solution 2

Add font folder inside res folder like below image

enter image description here

And directly add font family into your textview

<androidx.appcompat.widget.AppCompatTextView
    android:id="@+id/txtTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/app_name"
    android:fontFamily="@font/roboto_medium"
    android:textColor="@color/black"
    android:textSize="@dimen/_20ssp"
    android:visibility="gone" />

I hope this can help you!

Thank You.

like image 148
Hardik Talaviya Avatar answered Nov 12 '22 12:11

Hardik Talaviya