Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set custom font on an android custom textview. Untested code, be sure to test.
public class TextHelper{
private static TypeFace typeface = null;
public static void setTypeface(Context context, TextView textview){
if(this.typeface == null){
this.typeface = Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
}
textview.setTypeface(face);
}
}
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
TextHelper.setTypeface(context, this);
}
}
@lminhphuongtma

This comment has been minimized.

Copy link

@lminhphuongtma lminhphuongtma commented Aug 16, 2016

Line 9 : textview.setTypeface(typeface) is more exactly

@traendy

This comment has been minimized.

Copy link

@traendy traendy commented May 12, 2017

so smth like this?
`
class TextHelper{

private  static Typeface  typeface = null;

public static void setTypeface(Context context, TextView textview){
    if(typeface == null){
        typeface = Typeface.createFromAsset(context.getAssets(), "Helvetica_Neue.ttf");
    }
    textview.setTypeface(typeface);
}

}

public class CustomTextView extends TextView {

public CustomTextView(Context context){
    super(context);
    TextHelper.setTypeface(context, this);

}
public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TextHelper.setTypeface(context, this);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TextHelper.setTypeface(context, this);
}
protected void onDraw (Canvas canvas) {
    super.onDraw(canvas);


}

}
`

@qwerty9849

This comment has been minimized.

Copy link

@qwerty9849 qwerty9849 commented Jul 4, 2017

Nice , Thanks

@shahrukhbaig786

This comment has been minimized.

Copy link

@shahrukhbaig786 shahrukhbaig786 commented Mar 26, 2018

Great !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment