public class FontChangeCrawler {
private Typeface typeface;
public FontChangeCrawler(Typeface typeface) {
this.typeface = typeface;
}
public FontChangeCrawler(AssetManager assets, String assetsFontFileName) {
typeface = Typeface.createFromAsset(assets, assetsFontFileName);
}
public void replaceFonts(ViewGroup viewTree) {
View child;
for (int i = 0; i < viewTree.getChildCount(); ++i) {
child = viewTree.getChildAt(i);
if (child instanceof ViewGroup) {
replaceFonts((ViewGroup)child);
}
else if (child instanceof TextView) {
((TextView) child).setTypeface(typeface);
}
}
}
}
public class MainActivity extends AppCompatActivity {
@Override
public void setContentView(View view) {
super.setContentView(view);
FontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "fonts/myfont.otf");
fontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content));
}
}