Skip to content

Instantly share code, notes, and snippets.

@acappelli
Created March 25, 2016 08:03
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 acappelli/df4f375311e5d4d9591a to your computer and use it in GitHub Desktop.
Save acappelli/df4f375311e5d4d9591a to your computer and use it in GitHub Desktop.
/**
* @author Andrea Cappelli
*/
public class CustomTextView extends TextView {
private static final String TAG = CustomTextView.class.getName();
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context, attrs);
}
private void setCustomFont(Context ctx, AttributeSet attrs) {
TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
String customFont = a.getString(R.styleable.CustomTextView_customFont);
setCustomFont(ctx, customFont);
a.recycle();
}
public boolean setCustomFont(Context ctx, String asset) {
Typeface typeface = null;
try {
typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
} catch (Exception e) {
LogHelper.e(TAG, "Unable to load typeface: " + e.getMessage());
return false;
}
setTypeface(typeface);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment