Skip to content Skip to sidebar Skip to footer

Runtimeexception: Native Typeface Cannot Be Made Or Memory Leak For Custom Textview Loading Font

There's a HUGE problem in my code wherein I am loading a font in my assets\fonts\ folder from a custom TextView class. The first problem is that it crashes on 4.0 devices with the

Solution 1:

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:

publicclassTypeFaces{

    privatestaticfinal Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    publicstatic 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.");
                    returnnull;
                }
            }
            return cache.get(assetPath);
        }
    }
}

And the custom TextView class:

publicclassTextViewHirakakuextendsTextView {

    publicTextViewHirakaku(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    publicTextViewHirakaku(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    publicTextViewHirakaku(Context context) {
        super(context);
    }

    publicvoidsetTypeface(Typeface tf, int style) {
        if (style == Typeface.BOLD) {
            super.setTypeface(TypeFaces.getTypeFace(getContext(),
                    "fonts/hirakakupronbold.ttf"));
        } elseif (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.

Solution 2:

If you're having this issue on Android Studio then put your assets under main directory instead of putting it under res directory.

Also in font naming use lowercase letters and underscores only, e.g. my_font.ttf

That worked like charm for me

Solution 3:

If you are extending this view from xml then try using it this way::

publicclassMyTextViewextendsTextView {

  publicMyTextView(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      init();
  }

 publicMyTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      init();
  }

 publicMyTextView(Context context) {
      super(context);
      init();
 }


publicvoidinit() {
    Typefacetf= Typeface.createFromAsset(getContext().getAssets(), "fonts/hirakakupronbold.ttf");
    setTypeface(tf);

}

}

Its working fine for me. Make separate class extending TextView for each typeface style.to To apply it, replace your "TextView" with "com.yourpackage.MyTextView"

Regards,

Solution 4:

In my case, the XML namespace prefix that I was using for my custom view (costum) wasn't set right:

xmlns:costum="http://schemas.android.com/apk/tools"

All I had to do is to change it to

xmlns:costum="http://schemas.android.com/apk/res-auto"

& it worked.

Post a Comment for "Runtimeexception: Native Typeface Cannot Be Made Or Memory Leak For Custom Textview Loading Font"