Skip to content

Instantly share code, notes, and snippets.

@manojbhadane
Created July 11, 2018 15: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 manojbhadane/8fc98d7a3923e3aed561f73bd06a1638 to your computer and use it in GitHub Desktop.
Save manojbhadane/8fc98d7a3923e3aed561f73bd06a1638 to your computer and use it in GitHub Desktop.
public class CustomTextView extends AppCompatTextView {
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setFont(context, attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFont(context, attrs);
}
private void setFont(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
int customFont = a.getInt(R.styleable.CustomTextView_fontname, 0);
String fontname = "";
switch (customFont) {
case 1:
fontname = "Ubuntu-R.ttf";
break;
case 2:
fontname = "Ubuntu-B.ttf";
break;
case 3:
fontname = "Ubuntu-LI.ttf";
break;
}
setFont(fontname);
}
public boolean setFont(String asset) {
Typeface typeface = null;
try {
typeface = Typeface.createFromAsset(getContext().getResources().getAssets(), asset);
} catch (Exception e) {
return false;
}
setTypeface(typeface);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment