Iterate through views in layout and change font

I am wondering if there is a way to repeat all the views in the layout and change the font of all types having text (i.e. TextView, CheckBox, EditText, etc.). I have a layout that I call setContentView (), and wondered if there is an easy way to do this.

I could go through and manually do this with findViewById (), but I would prefer a simple way to iterate through them.

Thanks, -clark -

+7
source share
1 answer

Perhaps this will help you:

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); } } } 
+17
source

All Articles