Hello: Who dabbled with fonts and put them in the application: I do this: folder: assets/fonts/mistra.ttf in the class to which xml is attached I write

 TextView tv; Typeface face; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a1); tv = (TextView) findViewById(R.id.eda); face = Typeface.createFromAsset(getAssets(), "fonts/mistral.ttf"); tv.setTypeface(face); } 

and I see the following error: native typeface cannot be made refers to the string

 face = Typeface.createFromAsset(getAssets(), "fonts/mistral.ttf"); 
  • Is the problem specifically with this font, or any? - Vyacheslav Martynenko
  • And the assets are located under the main or resources directory? - Vyacheslav Martynenko
  • @Vyacheslav Martynenko others have not tried the folder app \ src \ main \ assets - upward
  • Try the src/main/assets/mistral.ttf , and use its "mistral.ttf" . Also try other fonts. - Vyacheslav Martynenko
  • And the font mistrial.ttf there exactly lies? You have font.ttf in question. Try another font of some kind. - artemiygreg

1 answer 1

You most likely have a memory leak. It will correctly load this way Utils.getTypeFace(ctx, "fonts/mistral.ttf")

 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); } } } 

In this case, you will protect your application from leakage, since the font will be loaded once and for the entire session of the application.

UPD

 tv = (TextView) findViewById(R.id.eda); face = Utils.getTypeFace(this, "fonts/mistral.ttf"); tv.setTypeface(face); 
  • But you can instructions for your example) - upward
  • Create a Utils class or whatever. place in it the code described above. Replace your line with face = Typeface.createFromAsset (getAssets (), "fonts / mistral.ttf"); on face = Utils.getTypeFace (this, "fonts / mistral.ttf"); - Chaynik
  • @upword updated - Chaynik
  • Please tell me, and if I have several activities, I’ll write tv = (TextView) in each of findViewById (R.id.eda); face = Utils.getTypeFace (this, "fonts / mistral.ttf"); tv.setTypeface (face); - upward
  • one
    @upward stackoverflow.com/a/7197867/2667883 here is an example of this link, - Chaynik