Question

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-

Was it helpful?

Solution

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

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top