Skip to content

Instantly share code, notes, and snippets.

@bblackbelt
Created July 11, 2015 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bblackbelt/c6698693556b71c4ff31 to your computer and use it in GitHub Desktop.
Save bblackbelt/c6698693556b71c4ff31 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="FontTextView">
<attr name="fontName" format="string"/>
</declare-styleable>
</resources>
public class FontTextView extends TextView {
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView);
setFont(a.getString(R.styleable.FontTextView_fontName);
a.recycle();
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FontTextView);
setFont(a.getString(R.styleable.FontTextView_fontName);
a.recycle();
}
public FontTextView(Context context) {
super(context);
}
public void setFont(String fontName) {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), fontName);
setTypeface(tf);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment