Skip to content

Instantly share code, notes, and snippets.

@achinverma
Last active September 20, 2018 07:52
Show Gist options
  • Save achinverma/771a0474747297c5cdbc6824103c26cc to your computer and use it in GitHub Desktop.
Save achinverma/771a0474747297c5cdbc6824103c26cc to your computer and use it in GitHub Desktop.
//Why don't you keep the created typface object in memory so that you don't create every time the text view is getting created.
//Following is a sample class that creates and cache the typeface object:
public class TypeFaceProvider {
public static final String TYPEFACE_FOLDER = "fonts";
public static final String TYPEFACE_EXTENSION = ".ttf";
private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>(
4);
public static Typeface getTypeFace(Context context, String fileName) {
Typeface tempTypeface = sTypeFaces.get(fileName);
if (tempTypeface == null) {
String fontPath = new StringBuilder(TYPEFACE_FOLDER).append('/').append(fileName).append(TYPEFACE_EXTENSION).toString();
tempTypeface = Typeface.createFromAsset(context.getAssets(), fontPath);
sTypeFaces.put(fileName, tempTypeface);
}
return tempTypeface;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment