class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> CACHE = new Hashtable<>();
private Typefaces() {
}
/**
* Get one typeface per application.
* @param context - context
* @param assetPath - path to asset folder
* @return - The {@link Typeface} to use
*/
public static Typeface get(Context context, String assetPath) {
synchronized (CACHE) {
if (!CACHE.containsKey(assetPath)) {
try {
Typeface typeface = Typeface.createFromAsset(context.getResources().getAssets(),
assetPath);
CACHE.put(assetPath, typeface);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath
+ "' because " + e.getMessage());
return null;
}
}
return CACHE.get(assetPath);
}
}
}
/**
* Set the custom font.
* @param ctx - context
* @param asset - asset to load.
* @return true if set, false otherwise.
*/
public boolean setCustomFont(Context ctx, String asset) {
Typeface tf = null;
try {
tf = Typefaces.get(ctx, asset);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface: " + e.getMessage());
}
if (tf != null) {
setTypeface(tf);
return true;
}
return false;
}