BIW BIW - 19 days ago 9
Android Question

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 in question.

The format of the directory I'm using is
main > assets > fonts > Portrait-Light.ttf


screenshot of directory

I've looked everywhere for a solution but they all seem to be rounding to the same answers on SO.

CustomFontTextView.java :

public class CustomFontTextView extends TextView {

public CustomFontTextView(Context context) {
super(context);
applyCustomFont(context);
}

public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
applyCustomFont(context);
}

public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
applyCustomFont(context);
}

private void applyCustomFont(Context context) {
Log.e("it gets here", "custom font");
Typeface customFont = FontCache.getTypeface("Roboto-Italic.ttf", context);
setTypeface(customFont);
}
}


FontCache.java

class FontCache {

private static HashMap<String, Typeface> fontCache = new HashMap<>();

static Typeface getTypeface(String fontname, Context context) {
Typeface typeface = fontCache.get(fontname);
if (typeface == null) {
try {
typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
} catch (Exception e) {
Log.e("failed", e.getMessage());
}
fontCache.put(fontname, typeface);
}
return typeface;
}
}


XML:

<icn.premierandroid.misc.CustomFontTextView
android:id="@+id/switch_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="14sp"
android:textColor="@color/colorPrimary"
android:gravity="center_horizontal"
android:text="@string/are_you_over_18_years_old"/>


I have tried this with different formats such as
.otf
and with different fonts such as
Roboto-Italic.ttf
and another random one from dafont.com called
Sunset-Clouds.ttf
but still i get the error message, what is going on? this should be working. I've even updated all plugins such as Gradle, Grade-Wrapper distribution and Android gradle plugin just in case.

I have also tried the single way of doing it:

AssetManager am = context. getApplicationContext(). getAssets();
Typeface font = Typeface.createFromAsset(
am, String.format(Locale.US, "fonts/%s", "portrait-light.ttf"));


Am i missing something?

Update:

Removal of the catch reveals this stacktrace.


Process: icn.premierandroid, PID: 3829
java.lang.RuntimeException: Unable to start activity ComponentInfo{icn.premierandroid/icn.premierandroid.RegisterActivity}:
android.view.InflateException: Binary XML file line #100: Error
inflating class icn.premierandroid.misc.CustomFontTextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767)
.....

Caused by: android.view.InflateException: Binary XML file line #100:
Error inflating class icn.premierandroid.misc.CustomFontTextView
at android.view.LayoutInflater.createView(LayoutInflater.java:640)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)

.....

Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:614)

....

Caused by: java.lang.RuntimeException: Font asset not found
fonts/GleegooRegular.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:272)
at icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21)
at icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28)
at icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18)
at java.lang.reflect.Constructor.newInstance(Native Method) 

.....


UPDATE 2:

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.

Answer

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.

Comments