Try this
MyApplication.java (This is Application class)
//Typeface public static Typeface poppinsSemiBold; public static Typeface helveticaNeueMedium; public static Typeface poppinsRegular; /** * Preload typefaces. */ private void preloadTypefaces() { poppinsSemiBold = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_SEMI_BOLD); helveticaNeueMedium = Typefaces.get(getApplicationContext(), Typefaces.FONT_HELVETICANEUE_MEDIUM); poppinsRegular = Typefaces.get(getApplicationContext(), Typefaces.FONT_POPPINS_REGULAR); }
call this preloadTypefaces() in onCreate() of same class MyApplication.java
Typefaces.java
import android.content.Context; import android.graphics.Typeface; import android.util.Log; import java.util.Hashtable; public class Typefaces { private static final String TAG = Typefaces.class.getSimpleName(); public static final String FONT_POPPINS_SEMI_BOLD = "fonts/Poppins_SemiBold.ttf"; public static final String FONT_HELVETICANEUE_MEDIUM = "fonts/HelveticaNeue_Medium.otf"; public static final String FONT_POPPINS_REGULAR = "fonts/Poppins_Regular.ttf"; private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>(); public static Typeface get(Context c, String assetPath) { synchronized (cache) { if (!cache.containsKey(assetPath)) { try { Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath); cache.put(assetPath, t); } catch (Exception e) { Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage()); return null; } } return cache.get(assetPath); } } }
Make sure your .ttf or .otf file is in assets/fonts/HelveticaNeue_Medium.otf added like that
res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomFontTextView"> <attr name="fontStyle" format="enum"> <enum name="poppinsSemiBold" value="1" /> <enum name="helveticaNeueMedium" value="2" /> <enum name="poppinsRegular" value="3" /> </attr> </declare-styleable> <declare-styleable name="TextViewPlus"> <attr name="customFont" format="string" /> </declare-styleable> </resources>
Custom Font class TextViewPlus.java
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Typeface; import android.util.AttributeSet; import android.util.Log; import android.widget.TextView; import com.offerbuk.R; import com.offerbuk.app.MyApplication; public class TextViewPlus extends TextView { private static final String TAG = "TextViewPlus"; private int fontStyle; private TypedArray a = null; public TextViewPlus(Context context) { super(context); init(null, 0); } public TextViewPlus(Context context, AttributeSet attrs) { super(context, attrs); init(attrs, 0); } public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs, defStyle); } private void init(AttributeSet attrs, int defStyle) { try { this.a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, defStyle, 0); this.fontStyle = a.getInteger(R.styleable.CustomFontTextView_fontStyle, 0); } finally { if (this.a != null) this.a.recycle(); } if (!isInEditMode()) { switch (fontStyle) { case 1: setTypeface(MyApplication.poppinsSemiBold); break; case 2: setTypeface(MyApplication.helveticaNeueMedium); break; case 3: setTypeface(MyApplication.poppinsRegular); break; default: setTypeface(MyApplication.poppinsRegular); break; } } } }
Use this in Your layout file
<TextViewPlus android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:fontStyle="poppinsRegular" />