Skip to content

Instantly share code, notes, and snippets.

@oguya
Created January 30, 2014 08:51
Show Gist options
  • Save oguya/8704814 to your computer and use it in GitHub Desktop.
Save oguya/8704814 to your computer and use it in GitHub Desktop.
set custom fonts in java
public class MyApplication extends Application {
private Typeface normalFont;
private Typeface boldFont;
//FontSetter fontSetter = (FontSetter) getApplication();
//TextView myTextView = (TextView) findViewById(R.id.my_textview);
//application.setTypeface(myTextView);
// -- Fonts -- //
public void setTypeface(TextView textView) {
if(textView != null) {
if(textView.getTypeface() != null && textView.getTypeface().isBold()) {
textView.setTypeface(getBoldFont());
} else {
textView.setTypeface(getNormalFont());
}
}
}
private Typeface getNormalFont() {
if(normalFont == null) {
normalFont = Typeface.createFromAsset(getAssets(),"fonts/my_font.ttf");
}
return this.normalFont;
}
private Typeface getBoldFont() {
if(boldFont == null) {
boldFont = Typeface.createFromAsset(getAssets(),"fonts/my_font_bold.ttf");
}
return this.boldFont;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment