Sets the TypeFace typeFace for all TextView 's in the view-hierarchy of ViewGroup parent. - Android User Interface

Android examples for User Interface:TextView Font

Description

Sets the TypeFace typeFace for all TextView 's in the view-hierarchy of ViewGroup parent.

Demo Code


//package com.java2s;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;

import android.view.ViewGroup;
import android.widget.TextView;

public class Main {
    private static boolean DO_SUBPIXEL_RENDERING = true;

    /**/*from  ww  w .  j ava  2 s .  c  o  m*/
     * Sets the {@link TypeFace} <code>typeFace</code> for all {@link TextView}'s in the view-hierarchy of {@link ViewGroup} <code>parent</code>.
     * 
     * @param typeFace
     * @param parent
     */
    public static void setTypeFace(Typeface typeFace, ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = parent.getChildAt(i);
            if (v instanceof ViewGroup) {
                setTypeFace(typeFace, (ViewGroup) v);
            } else if (v instanceof TextView) {
                TextView tv = (TextView) v;
                if (DO_SUBPIXEL_RENDERING) {
                    tv.setPaintFlags(tv.getPaintFlags()
                            | Paint.SUBPIXEL_TEXT_FLAG);
                }
                tv.setTypeface(typeFace);
            }
        }
    }

    /**
     * @see ViewUtil#setTypeFace(Typeface, ViewGroup)
     * 
     *  Sets the type face for n-views, but NOT for their children.
     * 
     * @param typeFace
     * @param views
     */
    public static void setTypeFace(Typeface typeFace, TextView... views) {
        for (TextView view : views) {
            view.setTypeface(typeFace);
            if (DO_SUBPIXEL_RENDERING) {
                System.out.println("do subpixel rendering");
                view.setPaintFlags(view.getPaintFlags()
                        | Paint.SUBPIXEL_TEXT_FLAG);
            }
        }
    }
}

Related Tutorials