Skip to content

Instantly share code, notes, and snippets.

@layerlre
Last active August 29, 2015 14:05
Show Gist options
  • Save layerlre/793f9386a38c19e30a33 to your computer and use it in GitHub Desktop.
Save layerlre/793f9386a38c19e30a33 to your computer and use it in GitHub Desktop.
Custom font textview
package com.layer.view;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class TextViewRoboto extends TextView {
public TextViewRoboto(Context context) {
super(context);
setFont();
}
public TextViewRoboto(Context context, AttributeSet attrs) {
super(context, attrs);
setFont();
}
public TextViewRoboto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont();
}
private void setFont() {
int style = Typeface.NORMAL;
try {
style = getTypeface().getStyle();
} catch (Exception e) {
// TODO: handle exception
}
Typeface face;
if (style == Typeface.NORMAL) {
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
} else if (style == Typeface.BOLD) {
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Black.ttf");
} else if (style == Typeface.ITALIC) {
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Italic.ttf");
} else {
face = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");
}
this.setTypeface(face);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment