Skip to content

Instantly share code, notes, and snippets.

@deepak786
Last active January 11, 2017 10:19
Show Gist options
  • Save deepak786/57e27b035a01d3c67f80 to your computer and use it in GitHub Desktop.
Save deepak786/57e27b035a01d3c67f80 to your computer and use it in GitHub Desktop.
How to use the custom font inside the entire app. This example is for how to use custom font for all textviews inside the app.
// add fonts inside the *assets/fonts* folder
// create a class named customTextView.java
public class customTextView extends TextView {
private static final String SCHEMA = "http://schemas.android.com/apk/res/android";
public customTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getStyle(attrs, context);
}
public customTextView(Context context, AttributeSet attrs) {
super(context, attrs);
getStyle(attrs, context);
}
public customTextView(Context context) {
super(context);
getStyle(null, context);
}
// get the style of the textview and apply the font accordingly.
private void getStyle(AttributeSet attr, Context context){
int textStyle = 0;
Typeface tf = null;
if(attr!=null){
textStyle = attr.getAttributeIntValue(SCHEMA, "textStyle", 0);
}
switch (textStyle) {
case Typeface.NORMAL:
// textstyle normal case and set the normal custom font.
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/normal.ttf");
break;
case Typeface.BOLD:
// textstyle bold case and set the bold custom font.
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bold.ttf");
break;
case Typeface.ITALIC:
// textstyle italic case and set the italic custom font.
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/italic.ttf");
break;
case Typeface.BOLD_ITALIC:
// textstyle bold_italic case and set the bold_italic custom font.
tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/bold_italic.ttf");
break;
default:
break;
}
// method to set the typeface
setTypeface(tf);
}
}
//**************XML CODE****************
1. Normal font
<com.example.test.customTextView
android:id="@+id/hj1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hj2"
android:text="Custom normal" />
2. Bold font
<com.example.test.customTextView
android:id="@+id/hj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hj1"
android:text="custom bold"
android:textStyle="bold" />
3. Bold_Italic font
<com.example.test.customTextView
android:id="@+id/hj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hj1"
android:text="custom bold"
android:textStyle="bold|italic" />
4. italic font
<com.example.test.customTextView
android:id="@+id/hj"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/hj1"
android:text="custom bold"
android:textStyle="italic" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment