Question

Is there away to set the type face to all the views(including the list view) at one time, instead of doing that for each view.thank you

Était-ce utile?

La solution

You can use the following method to set typeface to the layouts

public void setFont(ViewGroup group, Typeface font) {
        int count = group.getChildCount();
        View v;
        for (int i = 0; i < count; i++) {
            v = group.getChildAt(i);
            if (v instanceof TextView || v instanceof EditText || v instanceof Button) {
                ((TextView) v).setTypeface(font);
            } else if (v instanceof ViewGroup)
                setFont((ViewGroup) v, font);
        }
    }

Autres conseils

Use this code :

public static void APPLY_FONT(final Context context, final View root,
        final String fontName) {
    try {
        if (root instanceof ViewGroup) {
            ViewGroup viewGroup = (ViewGroup) root;
            for (int i = 0; i < viewGroup.getChildCount(); i++)
                APPLY_FONT(context, viewGroup.getChildAt(i), fontName);
        } else if (root instanceof TextView)
            ((TextView) root).setTypeface(Typeface.createFromAsset(
                    context.getAssets(), fontName));
    } catch (Exception e) {
        Log.e("ProjectName", String.format(
                "Error occured when trying to apply %s font for %s view",
                fontName, root));
        e.printStackTrace();
    }
}

Pass the Outer Layout id and font name.

Everybody can use this code :

public static void overrideFonts(final Context context, final View v, String fonts) {
    try {
        if (v instanceof ViewGroup) {
            ViewGroup vg = (ViewGroup) v;
            for (int i = 0; i < vg.getChildCount(); i++) {
                View child = vg.getChildAt(i);
                overrideFonts(context, child, fonts);
            }
        }else if (v instanceof EditText) {
            ((EditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof Button) {
            ((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof TextView) {
            ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof SwitchCompat) {
            ((SwitchCompat) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof AppCompatEditText) {
            ((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof Spinner) {
            ((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }else if (v instanceof AppCompatButton) {
            ((AppCompatEditText) v).setTypeface(Typeface.createFromAsset(context.getAssets(), fonts));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top