Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Runtime Exception font asset not found

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

like image 904
Muzammil Husnain Avatar asked Nov 24 '15 07:11

Muzammil Husnain


4 Answers

Use this method :

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

ResourcesCompat class is a compatible way to retrieve your resources.

like image 160
smarteist Avatar answered Nov 21 '22 00:11

smarteist


  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...
like image 35
Tiferet Cohen Avatar answered Nov 20 '22 22:11

Tiferet Cohen


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

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
like image 7
Gowtham Raj Avatar answered Nov 21 '22 00:11

Gowtham Raj


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");
like image 5
Midhun MP Avatar answered Nov 20 '22 22:11

Midhun MP