Monday, February 13, 2012

How to Set a custom font to entire layout in android?

If you want to change the font of all views in a layout, you have to traverse the entire layout recursively

protected void changeFonts(ViewGroup root) {
   
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/comicsans.ttf");
   
    for(int i = 0; i <root.getChildCount(); i++) {
    View v = root.getChildAt(i);
    if(v instanceof TextView ) {
    ((TextView)v).setTypeface(tf);
    } else if(v instanceof Button) {
    ((Button)v).setTypeface(tf);
    } else if(v instanceof EditText) {
    ((EditText)v).setTypeface(tf);
    } else if(v instanceof ViewGroup) {
    changeFonts((ViewGroup)v);
    }
    }
   
    }

No comments:

Post a Comment