Also, consider how you might handle widgets whose typeface you don't want to change. I will leave handling the ActionBar as an exercise for the reader. ĬonvertView = inflater.inflate(R.layout.listitem, null) įontChanger.replaceFonts((ViewGroup)convertView) Therefore, you will also need to use the FontChangeCrawler in your adapters. The list items in a ListView are built within an adapter, not within an Activity. Replacing Activity fonts goes a long way, but most of us also have a plethora of ListViews. Super.onActivit圜reated(savedInstanceState) įontChanger.replaceFonts((ViewGroup) this.getView()) void onActivit圜reated(Bundle savedInstanceState) Consider placing this logic in a BaseFragment class. You will need to apply the FontChangeCrawler to each Fragment as well. If you are not familiar with android.R.id.content, it is the official ID given to the root View within an Activity's layout.Ĭonsider placing the above logic in a BaseActivity class. To replace the default font in every view within an Activity's layout, simply use the FontChangeCrawler from above, like so: void setContentView(View view)įontChangeCrawler fontChanger = new FontChangeCrawler(getAssets(), "font.otf") įontChanger.replaceFonts((ViewGroup)this.findViewById(android.R.id.content)) ((TextView) child).setTypeface(typeface) For example, to access a font resource, we use font/myfont, or R.font.myfont. We can access the font resources with the help of a new resource type, font. These fonts are compiled in our R file and are automatically available in Android Studio. Public void replaceFonts(ViewGroup viewTree)įor(int i = 0 i < viewTree.getChildCount() ++i) We can add the font file in the res/font/ folder to bundle fonts as resources. Typeface = Typeface.createFromAsset(assets, assetsFontFileName) Public FontChangeCrawler(AssetManager assets, String assetsFontFileName) Public FontChangeCrawler(Typeface typeface) Here is a simple recursive implementation of a view crawler that will replace the Typeface for any appropriate view in the hierarchy: public class FontChangeCrawler When crawling this tree, one can replace all the Typefaces on any TextViews, EditTexts, and Buttons that are encountered. Just before the closing brace of the onCreate method a Typeface object is created and loaded with a font from assets. This tree can be crawled by visiting child views in your favorite order (breadth first or depth first). Select New then Folder then Assets Folder. Layouts (and sub-layouts) in Android are tree hierarchies comprised of ViewGroups as composite elements and Views as leaf nodes. Here is what I did: package de. import import import import public class. This post covers a strategy that should handle your needs for common apps. Well I couldnt figure out how to do it with the available classes so I extended the TypefaceSpan on my own an now it works for me. Instead you must employ a strategy to set a custom Typeface on all TextViews, EditTexts, and Buttons. Android does not provide a mechanism for using a custom font file (TTF, OTF, etc) in all areas of your app.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |