Set view's font style - Android User Interface

Android examples for User Interface:View

Description

Set view's font style

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.Typeface;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Main {
    /**/*ww  w.  j  av  a2s.c om*/
     * Set view's font style
     * 
     * @param ctx
     *       The activity or app context
     * @param subPathToFont
     *       The file name of the font data in the assets directory
     * @param views
     *       Only can be the kind of view which is able to set font style
     */
    public static void setFontStyle(Context ctx, String subPathToFont,
            View... views) {

        if (views.length > 0) {

            Typeface face = Typeface.createFromAsset(ctx.getAssets(),
                    subPathToFont);

            if (face != null) {

                for (View v : views) {
                    if (v instanceof TextView) {
                        ((TextView) v).setTypeface(face);
                    } else if (v instanceof Button) {
                        ((Button) v).setTypeface(face);
                    }
                }
            }
        }
    }
}

Related Tutorials