I downloaded the ttf font and put it in the assets / fonts folder .. full folder address: app \ src \ main \ assets \ fonts

I pull out in activit:

Typeface myfonts = Typeface.createFromAsset(getAssets(), "fonts/a_AntiqueTitulGr.ttf"); mSelectedItemView.setTypeface(myfonts); 

It compiles fine, but the application gives an error. Looked at what: "native typeface cannot be made".

Found a solution that a memory leak is possible. How the solution created the class:

  public class Utils { private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); public static Typeface getTypeFace(Context context, String assetPath) { synchronized (cache) { if (!cache.containsKey(assetPath)) { try { Typeface typeFace = Typeface.createFromAsset(context.getAssets(), assetPath); cache.put(assetPath, typeFace); } catch (Exception e) { Log.e("TypeFaces", "Typeface not loaded."); return null; } } return cache.get(assetPath); } } } 

And now pulled out like this:

  Typeface myfonts = Utils.getTypeFace(this, "fonts/a_BentTitulDcFr.ttf"); mSelectedItemView.setTypeface(myfonts); 

The error has disappeared. The application has started. But the font does not change. How to solve this problem? Thank.

    2 answers 2

    They also write that sometimes this decision helps. Write in gradle

     sourceSets { main { assets.srcDirs = ['assets'] } 
    • No, it did not help - Alexey

    The problem was banal. You cannot use upper case and all hyphens in the font name.

     fonts/a_AntiqueTitulGr.ttf - не верно fonts/antiquetitulgr.ttf - верно 

    Do not forget to rename it in the fonts folder.