22
votes

Here is my code and screenshot I'm trying to set custom font typeface but Runtime exception occurs font asset not found while font file is in asset folder. Am I missing something ?

Typeface font = Typeface.createFromAsset(getAssets(), "font/terminal.ttf");
((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(font);

enter image description herescreenshot of android studio project

13
I had a similar error. Restarting Android Studio solved it. Don't ask me why.... - Christine
In my case even just restart simulator helps. Looks like a bug in android studio - Ph0en1x
Restarting Android studio (even in administrator) did not work for me, path of my folder is correct. - Denny

13 Answers

32
votes

Use this method :

final Typeface typeface = ResourcesCompat.getFont(context, R.font.X);

ResourcesCompat class is a compatible way to retrieve your resources.

14
votes
  1. Folder's name should be "fonts" and not "font"
  2. Note that your "fonts" folder is located under your "assets" folder (which should be located under your "main" folder and not your "res" folder) It took me way too long to figure this one out...
7
votes

the folder name has to be 'fonts' not 'font'

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
5
votes

Your font asset folder is named incorrectly. You should name the folder as fonts not as font. Also change your code:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/terminal.ttf");
3
votes

If you're using Instant Run with Android Gradle plugin version 2.2.0-alphaX, it is a known bug.

A workaround is to turn of Instant Run until the issue is resolved.

You can track it here: https://code.google.com/p/android/issues/detail?id=212849&can=1&q=subcomponent%3DTools-Studio%20-has%3Aproject%20attachments%3D0&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&start=7700

2
votes

Common error when you have assets in your project and you are using the alpha versions of AS. This appears to be a bug in the Android studio build system. A simple workaround is to clean the project before you run it and that should solve the issue that you are facing.

2
votes

I had the same problem and managed to fix it. Originally I thought the font files were corrupt but they weren't. Then I thought Android Studio didn't like .ttf files, because they were the only ones not working. Turns out it's nothing wrong with the fonts.

FIX: Just click Build > Clean project. Fixed it straight away for me.

0
votes

Typeface typeface = Typeface.createFromAsset(this.getAssets(),"font/terminal.ttf");

((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(typeface);

0
votes

I have tried another font file that worked fine So I conclude that earlier font file was corrupt. Thanks @Miduhun MP , @Gowtham Raj and @jagan reddy

0
votes

If you use AndroidAnnotations, in app build.gradle, verify if assets folder is ok: ex: main/src/assets.

If you change de font, uninstall your app from your device/emulator, and run again.

Code:

public static void setFontFace(Context context, TextView textView) {
  Typeface type = Typeface.createFromAsset(context.getAssets(), "myfont.ttf");
  textView.setTypeface(type);
}
0
votes

I had the problem that .woff fonts are not accepted on Android 7+. So i switched to .ttf fonts.

0
votes

For me, the font file itself was corrupted. I tried another one to make it work.

-3
votes

I have looked into all the answer but none of them worked for me. I found a new solution after reading the documentation. Here are the steps to follow:

  1. Go to file menu
  2. In new, go to Folder and create assets folder
  3. Paste your font file in this assets folder
  4. Use in your code using Typeface attribute.

    Typeface type = Typeface.createFromAsset(getAssets(), "myfont.ttf"); textView.setTypeface(type);

Now, you are all set to use the fonts you like.