Android Runtime Exception font asset not found

后端 未结 13 659
执念已碎
执念已碎 2021-01-04 01:11

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 som

相关标签:
13条回答
  • 2021-01-04 01:44

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

    Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
    
    0 讨论(0)
  • 2021-01-04 01:46
    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...
    0 讨论(0)
  • 2021-01-04 01:48

    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");
    
    0 讨论(0)
  • 2021-01-04 01:49

    Common error when you have assets in your project and you are using the alpha versions of AS. This appears to be a bug in the Android studio build system. A simple workaround is to clean the project before you run it and that should solve the issue that you are facing.

    0 讨论(0)
  • 2021-01-04 01:54

    If you use AndroidAnnotations, in app build.gradle, verify if assets folder is ok: ex: main/src/assets.

    If you change de font, uninstall your app from your device/emulator, and run again.

    Code:

    public static void setFontFace(Context context, TextView textView) {
      Typeface type = Typeface.createFromAsset(context.getAssets(), "myfont.ttf");
      textView.setTypeface(type);
    }
    
    0 讨论(0)
  • 2021-01-04 01:54

    I have looked into all the answer but none of them worked for me. I found a new solution after reading the documentation. Here are the steps to follow:

    1. Go to file menu
    2. In new, go to Folder and create assets folder
    3. Paste your font file in this assets folder
    4. Use in your code using Typeface attribute.

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

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

    0 讨论(0)
提交回复
热议问题