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-
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);
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With