0

Trying to set the font programmatically.

My file is located in src/main/res/fonts/material_font.ttf

circleProgress.setTextTypeface(Typeface.createFromAsset(assets, "material_font.ttf" ))

and i've tried

circleProgress.setTextTypeface(Typeface.createFromAsset(assets, "fonts/material_font.ttf" ))
circleProgress.setTextTypeface(Typeface.createFromAsset(assets, "font/material_font.ttf" ))
circleProgress.setTextTypeface(Typeface.createFromAsset(applicationContext.assets, "fonts/material_font.ttf" ))
...

The error is:

Font asset not found MaterialIcons-Regular.ttf

What in the world am I doing wrong?

Andrew Young
  • 684
  • 1
  • 13
  • 23
  • 2
    If you want to use `createFromAsset()`, your font would need to be in `src/main/assets/`. – CommonsWare Jan 22 '20 at 01:15
  • 1
    Just as @CommonsWare mentioned, you need to copy your font file to assets directory instead. If the folder does not already exists create it. You can take a look at this answer to a similar question too. https://stackoverflow.com/a/27588966/3475551 – Eaweb Jan 22 '20 at 01:23

2 Answers2

1

we look at the source code and document, for Type.createFromAsset

     /**
     * Create a new typeface from the specified font data.
     *
     * @param mgr  The application's asset manager
     * @param path The file name of the font data in the assets directory
     * @return The new typeface.
     */
public static Typeface createFromAsset(AssetManager mgr, String path)

so you should put the font.ttfin the asset file path

Lenoarod
  • 3,441
  • 14
  • 25
1

Consider doing this if you do not wish to save your font file to assets directory

Typeface typeface = ResourcesCompat.getFont(this, R.font.material_font.ttf);
circleProgress.setTextTypeface(typeface)
Eaweb
  • 811
  • 1
  • 12
  • 16