How to create custom view in Android and apply the custom font from xml.

Create custom textview or edittext with custom fonts

Hello Friends, After searching hundreds of answers related to creating the customtextview or edittext I came up with my implementation to efficiently create custom view so that we don’t have to assign typeface from java code.

And I call it efficient because once I came to scenario related to creating multiple fragments inside FragmentStatePagerAdapter and I wasted days solving the problem of UI lagging and freezing.And then I came to know that the view is inflating the typeface in wrong way.


Top 10 free Android libraries for app development in android studio

Lets start to Create custom textview or edittext with custom fonts.

First is your custom view class.It may be EditText or Button also.

public class TextViewCustom extends TextView {
    private static final String TAG = TextViewCustom.class.getSimpleName();

    public TextViewCustom(Context context) {

    public TextViewCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        parseAttributes(context, attrs); 

    public TextViewCustom(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        parseAttributes(context, attrs);

    private void parseAttributes(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomFont);
        String customFont = typedArray.getString(R.styleable.CustomFont_font);
        if (customFont != null && customFont.length() > 0) {
            setTypeface(TypeFaceProvider.getTypeFace(context, customFont));

Typeface Class to apply custom font to our view:

Be careful to put your font.ttf file inside main->assets->fonts->RobotoBold.ttf

public class TypeFaceProvider {

    public static final String TYPEFACE_FOLDER = "fonts";
    public static final String TYPEFACE_EXTENSION = ".ttf";

    private static Hashtable<String, Typeface> sTypeFaces = new Hashtable<String, Typeface>(

    public static Typeface getTypeFace(Context context, String fileName) {
        Typeface tempTypeface = sTypeFaces.get(fileName);

        if (tempTypeface == null) {
            String fontPath = new StringBuilder(TYPEFACE_FOLDER).append('/').append(fileName).append(TYPEFACE_EXTENSION).toString();
            tempTypeface = Typeface.createFromAsset(context.getAssets(), fontPath);
            sTypeFaces.put(fileName, tempTypeface);

        return tempTypeface;

content of values–> attrs.xml (imp)

<?xml version="1.0" encoding="utf-8"?>
    <declare-styleable name="CustomFont">
        <attr name="font" format="string" />


Include the view in your xml file:

Take care of the app:CustomFont .This name matches the name set for the value we defin

<?xml version="1.0" encoding="utf-8"?>

    android:orientation="vertical" android:layout_width="match_parent"

        app:font="@string/font_opensans_bold" />



Name of your font in values–> strings.xml file:

<!– Custom fonts text–>

<string name="font_opensans_bold">OpenSans-Bold</string>

You can get more idea about this custom view code from

Feeling glad to receive your comment