Created
August 1, 2017 08:05
-
-
Save addam01/92d843879fc1539b28c9f40699f179f4 to your computer and use it in GitHub Desktop.
Android Custom Textview
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
<declare-styleable name="CustomTextView"> | |
<attr name="fontName" format="string" /> | |
</declare-styleable> |
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 TextView { | |
public CustomTextView(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(attrs); | |
} | |
public CustomTextView(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(attrs); | |
} | |
public CustomTextView(Context context) { | |
super(context); | |
init(null); | |
} | |
private void init(AttributeSet attrs) { | |
if (attrs!=null) { | |
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CustomTextView); | |
String fontName = a.getString(R.styleable.CustomTextView_fontName); | |
if (fontName!=null) { | |
Typeface myTypeface = Typeface.createFromAsset(getContext().getAssets(), "fonts/"+fontName); | |
setTypeface(myTypeface); | |
} | |
a.recycle(); | |
} | |
} | |
} |
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
<com.abc.someclass.CustomTextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:fontName="/*name of your font from assets/font folder*/"/> |
Author
addam01
commented
Aug 1, 2017
- Create asset resource
- Copy Pasta your custom font file into the asset
- Create the Java class handler for custom text view
- Declare in the attrs.xml
- Use the custom in activity xml
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment