Skip to content Skip to sidebar Skip to footer

Runtime Exception: Font Not Found For Every Font I've Tried - Android

I have a custom extended TextView I am using for custom fonts within my application, but for some reason I keep getting a run-time exception where the program can't find the font i

Solution 1:

I think the real problem is that you haven't configured your assets folder. If you had, it would look like this in Project view:

Asset folder with pictogram

Or like this in Android view:

Asset folder with pictogram, Android view

So you should simply add this folder as assets folder in Studio: Click on your project structure window (or press Alt+1), then press Alt + Insert and select Folder/Assets Folder. Then put your fonts there.

Update from OP:

Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.

Solution 2:

We wrote this class for our needs. It allows to set our custom fonts using attribute, like this:

app:customFont="Roboto-Medium.ttf"

Hope you find it useful:

publicclassTextViewFontedextendsTextView {
    privatestaticfinalStringTAG="TextViewFonted";

    publicTextViewFonted(Context context) {
        super(context);
    }

    publicTextViewFonted(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    publicTextViewFonted(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    privatevoidsetCustomFont(Context ctx, AttributeSet attrs) {
        TypedArraya= ctx.obtainStyledAttributes(attrs, R.styleable.TextViewFonted);
        StringcustomFont= a.getString(R.styleable.TextViewFonted_customFont);
        if (customFont == null) customFont = "Roboto-Regular.ttf";
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    publicbooleansetCustomFont(Context ctx, String asset) {
        Typefacetf=null;
        try {
            tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            Log.e(TAG, "Could not get typeface", e);
            returnfalse;
        }

        setTypeface(tf);  
        setLineSpacing(getTextSize() * 0.3f, 0.75f);
        returntrue;
    }
}

And to be able to use this custom attribute, you should add to your attrs.xml next block (so R.styleable.TextViewFonted_customFont will work):

<declare-styleablename="TextViewFonted"><attrname="customFont"format="string"/></declare-styleable>

In our case, we put fonts in the root of assets folder, but you could change this behavior in method setCustomFont()

Update

To add this font, for example, we use

Screenshot of fonts that are in assets folder

app:customFont="Comfortaa-Bold.ttf"

Solution 3:

Try this....

put .ttf fonts in app > assets > fonts > Roboto-Italic.ttf

And then in onCreate method,

TypefaceRoboto= Typeface.createFromAsset(getAssets(),
            "fonts/Roboto-Italic.ttf");

and set font in textview,etc

textview.setTypeface(Roboto);

Post a Comment for "Runtime Exception: Font Not Found For Every Font I've Tried - Android"