So basically, I'm trying to change the font on all the views of a xml layout. This can only be done through a non-xml approach. For some reason, setFont will not work with my inflated view group child. I think I'm going about the viewGroup wrong... How could I instantiate this better? The code works when I use a regular view like a button, but I dont want to define a million buttons and textviews so my solution was to make a viewgroup created from the layout and to iterate through all the views in it to change the font. Ug please help!
public static void applyFonts(final View v, Typeface fontToSet) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
applyFonts(child, fontToSet);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(fontToSet);
} catch (Exception e) {
e.printStackTrace();
}
}
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null);
Typeface rockwellFont = Typeface.createFromAsset(getAssets(), "Rockwell.ttf");
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ViewGroup vg = (ViewGroup) inflater.inflate(R.layout.main, null);
applyFonts(vg,rockwellFont);
setContentView(vg);