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:
your_font.tff
under app/src/main/res/font
Typeface font = ResourcesCompat.getFont(context, R.font.your_font);
android:fontFamily="@font/your_font"
This could be due to a number of issues. Please see which one works!
The font file is corrupted. Please see whether the app works with another standard ttf font.
Sometimes Android Build studio shows up bugs. Try cleaning the project and building again.
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.