Set custom font to TextView.
You have two ways, first see first way:
1. Add your font files to assets folder, if not exist assets folder create its.
2. Create CustomTypeface class, for examples:
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Typeface;
public class CustomTypeface {
private static Typeface typefaseArialCondIt;
private static Typeface typefaseArialBold;
private static Typeface typefaseArialRegular;
public CustomTypeface() {
}
public CustomTypeface(Context context) {
AssetManager assets = context.getAssets();
CustomTypeface.typefaseArialCondIt = Typeface.createFromAsset(assets, "fonts/ArialCondIt.otf");
CustomTypeface.typefaseArialBold = Typeface.createFromAsset(assets, "fonts/ArialBold.otf");
CustomTypeface.typefaseArialRegular = Typeface.createFromAsset(assets, "fonts/ArialRegular.otf");
}
public static Typeface gettypefaseArialCondIt() {
return typefaseArialCondIt;
}
public static Typeface settypefaseArialCondIt(Typeface typefaseArialCondIt) {
return CustomTypeface.typefaseArialCondIt = typefaseArialCondIt;;
}
public static Typeface gettypefaseArialBold() {
return typefaseArialBold;
}
public static Typeface settypefaseArialBold(Typeface typefaseArialBold) {
return CustomTypeface.typefaseArialBold = typefaseArialBold;;
}
public static Typeface gettypefaseArialRegular() {
return typefaseArialRegular;
}
public static void settypefaseArialRegular(Typeface typefaseArialRegular) {
CustomTypeface.typefaseArialRegular = typefaseArialRegular;
}
}
3. And set custom font to TextView:
TextView yourTextView = (TextView) findViewById(R.id.textView1);
yourTextView.setTypeface(CustomTypeface.gettypefaseArialRegular);
Or second way, create custom TextView with your fonts:
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class ArialTextView extends TextView {
public ArialTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ArialTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ArialTextView(Context context) {
super(context);
}
@Override
public void setTypeface(Typeface tf, int style) {
if (style == Typeface.BOLD) {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialBold.otf"));
} else if (style == Typeface.NORMAL) {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialRegular.otf"));
} else if (style == Typeface.ITALIC) {
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ArialCondIt.otf"));
}
}
}
And in the layout xml you can create custom TextView, for example:
<com.your.package.ArialTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="TextView with your custom font"/>