1
votes

When I try to programatically set custom font from .ttf file in assets to button in my android app it returns error Caused by: java.lang.RuntimeException: Font asset not found fonts/menubutton.ttf. Assets folder is in main directory, and I use this code: Typeface tpf = Typeface.createFromAsset(this.getAssets(), "fonts/menubutton.ttf"); benterday.setTypeface(tpf); How can I repair this error?

3
did u put the font file inside of the assets/fonts folder? - letsCode

3 Answers

14
votes

There is a new way to accomplish this:

  • put your_font.tff under app/src/main/res/font
  • create Typefaces using Typeface font = ResourcesCompat.getFont(context, R.font.your_font);
  • access font from xml layouts with android:fontFamily="@font/your_font"

source

0
votes

This could be due to a number of issues. Please see which one works!

  1. The font file is corrupted. Please see whether the app works with another standard ttf font.

  2. Sometimes Android Build studio shows up bugs. Try cleaning the project and building again.

  3. Verify the font folder and the font file name (no spaces etc.)

0
votes

There is 2 ways to add fonts in android studio.

first way: right click on you app folder and choose: app->New->Folder->Assets Folder

then in newly created folder right click and then selected "New->Directory" and set the name to "fonts".

now you can add your fonts here and your problem would be fixed.

second way is @andreaciri way that works in newer versions of android studio.