If you constantly shove your font everywhere in all TextView , then why not make it a permanent attribute on TextView itself? The easiest option Create a new class and inherit it from TextView
public class ExTextView extends TextView { public ExTextView(Context context) { super(context); } public ExTextView(Context context, AttributeSet attrs) { super(context, attrs); parseAttributes(context, attrs); } public ExTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); parseAttributes(context, attrs); } private void parseAttributes(Context context, AttributeSet attrs) { TypedArray values = context.obtainStyledAttributes(attrs, R.styleable.ExTextView); int typeface = values.getInt(R.styleable.ExTextView_typeface, 0); setTypeface(FontsUtils.getTypeFace(context, typeface)); values.recycle(); } }
Create attrs.xml file in values or add it if you already have one.
<declare-styleable name="ExTextView"> <attr name="typeface" /> </declare-styleable> <attr name="typeface" format="enum"> <enum name="FONTS1" value="0" /> <enum name="FONTS2" value="1" /> <enum name="FONTS3" value="2" /> </attr>
Well, the class for downloading the font itself
public class FontsUtils { public final static int FONTS1 = 0; public final static int FONTS2 = 1; public final static int FONTS3 = 2; private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); public static Typeface getTypeFace(Context context, int typeFace) { synchronized (cache) { String assetPath; switch (typeFace) { case FONTS1: default: assetPath = "fonts/FONTS1.otf"; break; case FONTS2: assetPath = "fonts/FONTS2.otf"; break; case FONTS3: assetPath = "fonts/FONTS3.otf"; break; } if (!cache.containsKey(assetPath)) { try { cache.put(assetPath, Typeface.createFromAsset(context.getAssets(), assetPath)); } catch (Exception e) { Log.e("TypeFaces", "Typeface not loaded."); return null; } } return cache.get(assetPath); } } }
Now how does it all work
In all your xml with TextView , where you need to change the background, we change them to our custom, where you.package.name. You must replace the package name of your application.
<you.package.name.ExTextView android:layout_width="wrap_content" android:layout_height="wrap_content" />
If you have not explicitly indicated which font to use, the default font will be used in the default branches (see the FontsUtils class).
If you need to specify the font, use the app:typface (do not forget to add xmlns:app="http://schemas.android.com/apk/res-auto" to the root layout)
<you.package.name.ExTextView android:layout_width="wrap_content" android:layout_height="wrap_content" app:typeface="FONTS2" />
If for some reason you need to add the font programmatically:
TextView.setTypeFace(FontsUtils.getTypeFace(mContext, FontsUtils.FONTS2));
There will be questions to ask =) This is not a tricky way you will save yourself when adding new TextView , as the default will use the font you specified (in the example this is FONTS1 ).