Created
July 11, 2018 15:03
-
-
Save manojbhadane/8fc98d7a3923e3aed561f73bd06a1638 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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