Issue when using a custom font - native typeface cannot be made
The font file is either corrupt or unsupported for some reason. You can drop it on the SD card and load it from file, to make sure it's not a problem with your assets.
RuntimeException: native typeface cannot be made when loading font
Android does not support OpenType (OTF), only TrueType (TTF), so your Molot.otf
font probably will not work. I wrote both of those blog posts you link to in your opening sentence (the one is a pirated copy of the other), and they do not use Molot.otf
.
(BTW, I somewhat repaired the formatting of that post -- AndroidGuys keeps changing WordPress hosts, and so my older posts are terribly broken in terms of formatting).
EDIT: As stated in the comments, Android DOES now support OTF.
RuntimeException: native typeface cannot be made or memory leak for custom TextView loading font
Okay, so I finally figured that instantiating a TypeFace
object inside a TextView
class would cause so much load each time that same TextView
is instantiated. This caused my app to lag and resulted to OutOfMemoryException
eventually. So what I did was to create a different custom TypeFace
class that would call my fonts from the assets so that it instantiates from the TypeFace
class and not from the TextView
class.
Here's my TypeFaces class:
public class TypeFaces {
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 the custom TextView class:
public class TextViewHirakaku extends TextView {
public TextViewHirakaku(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TextViewHirakaku(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TextViewHirakaku(Context context) {
super(context);
}
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupronbold.ttf"));
} else if (style == Typeface.ITALIC) {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupronitalic.ttf"));
} else {
super.setTypeface(TypeFaces.getTypeFace(getContext(),
"fonts/hirakakupron.ttf"));
}
}
}
Notice that I'm now calling getTypeFace
method from TypeFaces
class here.
Custom Font Issue In Android
Android supports only TTF
font type not OTF
. Check this link
"RuntimeException: native typeface cannot be made" when loading font
Related Topics
Collections Sort(List<T>,Comparator<? Super T>) Method Example
Capture and Log the Response Body
How to Limit the Number of Characters in Jtextfield
Regex Look-Behind Without Obvious Maximum Length in Java
Mapping Manytomany with Composite Primary Key and Annotation:
Preventing System.Exit() from API
Sort Arrays of Primitive Types in Descending Order
Singleton Class with Several Different Classloaders
Can Not Find the Tag Library Descriptor for "Http://Java.Sun.Com/Jsp/Jstl/Core"
Using File.Listfiles with Filenameextensionfilter
Custom JSON Deserialization with Jackson
Background Timer Task in Jsp/Servlet Web Application
How to Use an Arraylist as a Prepared Statement Parameter
How to Cast a Double to an Int in Java by Rounding It Down
How to Solve the "Double-Checked Locking Is Broken" Declaration in Java