Vitthalk
Vitthalk

Reputation: 354

Android Custom Font issue

I am working on a android application. In this application, I am using Custom font (avenir_bold_font). I am using following method to set up font to buttons.

public static void setButtonFont(Activity activity, Button btnView, String fontType){
    Typeface tf = Typeface.createFromAsset(activity.getAssets(),"fonts/"+fontType);
    btnView.setTypeface(tf);
}

Using above method am able to set up font to a button without any trouble. But in above method i have creating Typeface object every time any other calls it.

So i have made the changes in above method, I have converted Typeface local variable to Static variable so that it will improve performance, But The moment i created Typeface object as static , it does not work. it does not able to set up font

Upvotes: 1

Views: 202

Answers (1)

Ted Hopp
Ted Hopp

Reputation: 234857

I'd suggest creating a utility class that caches your loaded typefaces—something like the following:

public class TypefaceCache {
    private final static Map<String, Typeface> cache = new HashMap<String, Typeface>();

    private TypefaceCache() { /* do not allow instance creation */ }

    public static Typeface getTypeface(Context context, String path){
        Typeface tf = cache.get(path);
        if (tf == null) {
            tf = Typeface.createFromAsset(context.getAssets(), path);
            cache.put(path, tf);
        }
        return tf;
    }
}

Then inside setButtonFont, retrieve your typeface from the cache instead of creating it directly from assets each time:

public static void setButtonFont(Button btn, String fontType){
    btn.setTypeface(TypefaceCache.getTypeface(btn.getContext, "fonts/" + fontType));
}

(You don't need the activity parameter because the button has a reference to the context.)

EDIT: After thinking about this a bit, I would suggest the following version of the cache:

public class TypefaceCache {
    private final static Map<String, SoftReference<Typeface>> cache
        = new HashMap<String, SoftReference<Typeface>>();

    private TypefaceCache() { /* do not allow instance creation */ }

    public static Typeface getTypeface(Context context, String path){
        synchronized (cache) {
            final SoftReference<Typeface> ref = cache.get(path);
            Typeface tf = ref == null ? null : ref.get();
            if (tf == null) {
                tf = Typeface.createFromAsset(context.getAssets(), path);
                cache.put(path, new SoftReference<Typeface>(tf));
            }
            return tf;
        }
    }
}

This has two improvements. First, it synchronizes on the cache object so that calls to getTypeface() are thread safe. (This may not be all that important if you are only going to call this method from the UI thread.) Second, it allows custom fonts to be garbage collected if the only reference to them is in the cache itself. With the original version I suggested, custom fonts would stay in memory for as long as the application process was alive, regardless of whether you still had a use for them.

Upvotes: 3

Related Questions