Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A TextView with a custom font.
/**
* @author Rany Albeg Wein
* @since 30/03/2018
* <p>
* // attrs.xml
* <declare-styleable name="CustomFontTextView">
* <attr name="customFont" format="string"/>
* </declare-styleable>
* // xml usage.
* <com.package.name.CustomFontTextView
* android:id="@+id/some_id"
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* app:customFont="fonts/Assistant-Bold.ttf"/>
*/
public class CustomFontTextView extends android.support.v7.widget.AppCompatTextView {
public CustomFontTextView(Context context) {
super(context);
}
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context, attrs);
}
private void setCustomFont(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView);
String customFont = a.getString(R.styleable.CustomFontTextView_customFont);
setCustomFont(context, customFont);
a.recycle();
}
public void setCustomFont(Context context, String asset) {
Typeface typeface;
typeface = Typeface.createFromAsset(context.getAssets(), asset);
setTypeface(typeface);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.