Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterate through views in a layout and change typeface

I'm wondering if there is a way to iterate through all the views in a layout and change the typeface of all the views which have text (i.e. TextView, CheckBox, EditText, etc). I have a layout which I call setContentView() and was wondering if there is an easy way to do this.

I could go through and manually do it using findViewById() but I'd rather have an easy way to iterate through them all instead.

Thanks, -clark-

like image 955
clark Avatar asked Sep 09 '11 19:09

clark


1 Answers

Maybe this will get you started:

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);
                }
        }

    }
like image 147
evilone Avatar answered Sep 19 '22 12:09

evilone