How to change the font on the TextView?
A1.
First download the .ttf
file of the font you need (arial.ttf
). Place it in the assets
folder(Inside assets folder create new folder named fonts and place it inside it). If txtyour is the textviews
you want to apply the font , use the following piece of code,
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf");
txtyour.setTypeface(type);
A2.
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/DroidSansFallback.ttf");
TextView tv = (TextView) findViewById(R.id.CustomFontText);
tv.setTypeface(tf);
A3.
public class Font {
public static final Font PROXIMA_NOVA = new Font("ProximaNovaRegular.otf");
public static final Font FRANKLIN_GOTHIC = new Font("FranklinGothicURWBoo.ttf");
private final String assetName;
private volatile Typeface typeface;
private Font(String assetName) {
this.assetName = assetName;
}
public void apply(Context context, TextView textView) {
if (typeface == null) {
synchronized (this) {
if (typeface == null) {
typeface = Typeface.createFromAsset(context.getAssets(), assetName);
}
}
}
textView.setTypeface(typeface);
}
}
And then to use in your activity...
myTextView = (TextView) findViewById(R.id.myTextView);
Font.PROXIMA_NOVA.apply(this, myTextView);
Comments
Post a Comment