26

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 here screenshot of android studio project

Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24

15 Answers15

61

Use this method :

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

ResourcesCompat class is a compatible way to retrieve your resources.

smarteist
  • 1,331
  • 12
  • 15
  • 2
    Hey there! While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Wing May 01 '19 at 16:58
  • This worked for me, should be marked as the correct answer – FabioR Mar 20 '20 at 15:46
  • Best answer. Simple, on-spot – zulkarnain shah Apr 13 '20 at 17:47
  • this should be the accepted answer, it's backwards compatible and avoids having magic strings – davy307 Nov 25 '20 at 14:57
17
  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...
Tiferet Cohen
  • 283
  • 2
  • 7
7

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

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
Gowtham Raj
  • 2,915
  • 1
  • 24
  • 38
5

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");
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
3

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

Laurențiu Onac
  • 471
  • 1
  • 7
  • 17
2

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.

Jishin Dev
  • 371
  • 7
  • 10
2

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.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
Will
  • 21
  • 1
2

worked for me

typefaceRegular = ResourcesCompat.getFont(mContext, R.font.sarala_regular);
            tv_bs_priceRange.setTypeface(typefaceRegular);
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
harry
  • 171
  • 1
  • 4
0

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

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

0

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

Muzammil Husnain
  • 1,218
  • 1
  • 10
  • 24
0

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);
}
Pierry
  • 979
  • 7
  • 17
0

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

bremen_matt
  • 6,902
  • 7
  • 42
  • 90
0

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

Bilal Halayqa
  • 932
  • 1
  • 6
  • 25
0

Try to access fonts just like this:

val font =ResourcesCompat.getFont(requireContext(), R.font.inter_light)

fazal ur Rehman
  • 330
  • 2
  • 5
-3

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.