Runtime source font not found

Here is my code and screenshot. I am trying to install my own font font, but a Runtime exception occurs if the font object is not found and the font file is in the resource folder. Did I miss something?

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

enter image description herescreenshot of android studio project

+10
java android android-fonts
source share
13 answers

I tried another font file that worked fine. Therefore, I came to the conclusion that the previous font file was damaged. Thanks @Miduhun MP, @Gowtham Raj and @jagan reddy

+1
source share

folder name should be 'fonts not' font '

 Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font); 
+8
source share

Your resource folder is incorrectly named. You must specify the folder as fonts not as font . Also change your code:

 Typeface font = Typeface.createFromAsset(getAssets(), "fonts/terminal.ttf"); 
+5
source share
  1. The folder name should be "fonts" and not "font"
  2. Please note that your "fonts" folder is located in the "assets" folder (which should be in your "main" folder, and not in the "res" folder). It took me too long to figure this out ...
+4
source share

If you are using Instant Run with Android version of Gradle version 2.2.0-alphaX, this is a known bug.

The workaround is to run Instant Run until the problem 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

+3
source share

A common mistake is when you have assets in your project and you use alpha versions of AS. This seems to be a bug in the android studio build system. A simple workaround is to clean up the project before it starts, and this should solve the problem you are facing.

+2
source share

I had the same problem and managed to fix it. Initially, I thought the font files were damaged, but they were not. Then I thought that Android Studio didn't like the .ttf files, because they were the only ones that didn't work. It turns out that fonts have nothing wrong.

FIX: Just click Build > Clean project . This is fixed for me.

+1
source share

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

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

0
source share

If you use AndroidAnnotations, in the app build.gradle make sure that the assets folder is in order: ex: main / src / assets.

If you change the font, uninstall the application from the device / emulator and run it again.

The code:

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

I'm having a problem with .woff fonts not being accepted on Android 7+. So I switched to .ttf fonts.

0
source share

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

0
source share

use this method:

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

I looked through the whole answer, but none of them worked for me. After reading the documentation, I found a new solution. Following are the following steps:

  • Go to the file menu
  • In the new version, go to the folder "Folder" and create a folder with resources
  • Paste the font file into this resource folder
  • Use in your code using the Typeface attribute.

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

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

-one
source share

All Articles