Why not define your custom TextView and assign it a font.
import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.EditText; import com.exmple.util.Utility; public class CustomFontEditText extends EditText { private Context mContext; private String ttfName; String TAG = getClass().getName(); public CustomFontEditText(Context context) { super(context); this.mContext = context; } public CustomFontEditText(Context context, AttributeSet attrs) { super(context, attrs); this.mContext = context;
Download and save the font from the resource in a hash map in your Utility class so that you can reuse the font later. Imagine loading a font every time a new View loads. OOM .. !!!
private static Map<String, Typeface> TYPEFACE = new HashMap<String, Typeface>(); public static Typeface getFonts(Context context, String fontName) { Typeface typeface = TYPEFACE.get(fontName); if (typeface == null) { typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+fontName); TYPEFACE.put(fontName, typeface); } return typeface; }
Add ttf attribute to set font from xml
public static final String ATTRIBUTE_TTF_KEY = "ttf_name"; public static final String ATTRIBUTE_SCHEMA = "http://schemas.android.com/apk/lib/com.exmaple.ui.customfont";
and now use in your layout file
<com.example.ui.customviews.CustomFontEditText xmlns:font="http://schemas.android.com/apk/lib/com.exmaple.ui.customfont" android:id="@+id/edt_pay_input" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:background="@null" android:cursorVisible="false" android:drawableBottom="@drawable/dashed_white" android:includeFontPadding="false" android:inputType="numberDecimal" android:maxLength="4" android:minWidth="20dp" android:singleLine="true" android:textColor="@color/white" android:textSize="30sp" font:ttf_name="FedraSansStd-Light.otf" > <requestFocus /> </com.example.ui.customviews.CustomFontEditText>
You can do the same for Button, TextView, etc.
source share