apply Font to View - Android Graphics

Android examples for Graphics:Font

Description

apply Font to View

Demo Code


//package com.java2s;
import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Main {
    public static void applyFont(final Context context, final View root,
            final String fontName) {
        Typeface font = Typeface.createFromAsset(context.getAssets(),
                fontName);//from  w w  w  .java2 s .co  m

        try {
            if (root instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) root;
                for (int i = 0; i < viewGroup.getChildCount(); i++)
                    applyFont(context, viewGroup.getChildAt(i), fontName);
            } else if (root instanceof TextView)
                ((TextView) root).setTypeface(font);
        } catch (Exception e) {
            Log.e("FontHelper",
                    String.format(
                            "Error occured when trying to apply %s font for %s view",
                            fontName, root));
            e.printStackTrace();
        }
    }
}

Related Tutorials