Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.HashMap; import android.content.Context; import android.graphics.Typeface; public class Main { private static final HashMap<String, Typeface> mCachedFonts = new HashMap<String, Typeface>(); /** * Returns typeface font from assets. If cached returns the cached value, otherwise loads from * assets and caches it. * * @param context * @param fontName * @return */ public static final Typeface getCachedFont(Context context, String fontName) { Typeface tf = null; Context appContext = context.getApplicationContext(); try { // check if the font is cached if (mCachedFonts.containsKey(fontName) && mCachedFonts.get(fontName) != null) { tf = mCachedFonts.get(fontName); } else { // get the font from assets tf = Typeface.createFromAsset(appContext.getAssets(), "fonts/" + fontName); // cache the font mCachedFonts.put(fontName, tf); } } catch (Exception e) { } return tf; } }