简体   繁体   中英

Font asset not found Android

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?

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

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.)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM