Custom Font In Android Xml {Best Solution}

Please follow and like us:

Custom Font In Android using AppCompat v7 Library

We all knows Appcompat v7 library is used for backward compatibility but there’s one more hidden features, we can create our own custom TextView, EditText,Button, CheckBox etc. by extending AppCompat Views.

Let’s Explore how to set custom font in Android using Appcompat

Add any fonts you liked in assets folder, here i puted Roboto Light Italic font.

Initialize Font in your Application class

import android.app.Application;
import android.graphics.Typeface;

public class MyApplication extends Application {

    private static Typeface robotoLightItalic;

    public static Typeface getRobotoLightItalic() {
        return robotoLightItalic;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        robotoLightItalic = Typeface.createFromAsset(getApplicationContext().getAssets(), "fonts/li.ttf");
    }
}

Don’t Forget to add Application class in androidMainifest.xml file.

   <application android:name=".MyApplication" ... ...>
        </application>

Create Custom TextView like bellow for custom font in android

import android.content.Context;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

public class RobotoLightItalicTextView extends AppCompatTextView {

    public RobotoLightItalicTextView(Context context) {
        super(context);
        setTypeface(context);
    }

    public RobotoLightItalicTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(context);
    }

    public RobotoLightItalicTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(context);
    }

    private void setTypeface(Context context) {
        if (context != null &amp;&amp; !isInEditMode()) {
            setTypeface(MyApplication.getRobotoLightItalic());
        }
    }
}

Create Custom EditText like bellow for custom font in android

import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;

public class RobotoLightItalicEditTextView extends AppCompatEditText {

    public RobotoLightItalicEditTextView(Context context) {
        super(context);
        setTypeface(context);
    }

    public RobotoLightItalicEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(context);
    }

    public RobotoLightItalicEditTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(context);
    }

    private void setTypeface(Context context) {
        if (context != null &amp;&amp; !isInEditMode()) {
            setTypeface(MyApplication.getRobotoLightItalic());
        }
    }
}

Create Custom Button like bellow for custom font in android

import android.content.Context;
import android.support.v7.widget.AppCompatButton;
import android.util.AttributeSet;

public class RobotoLightItalicButton extends AppCompatButton {

    public RobotoLightItalicButton(Context context) {
        super(context);
        setTypeface(context);
    }

    public RobotoLightItalicButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setTypeface(context);
    }

    public RobotoLightItalicButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setTypeface(context);
    }

    private void setTypeface(Context context) {
        if (context != null &amp;&amp; !isInEditMode()) {
            setTypeface(MyApplication.getRobotoLightItalic());
        }
    }
}

Now Use Custom Views instead of default views in your layouts

    <com.nkdroid.customviews.RobotoLightItalicTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="ROBOTO LIGHT ITALIC" android:textSize="14sp" />

    <com.nkdroid.customviews.RobotoLightItalicEditTextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="ROBOTO LIGHT ITALIC" android:padding="8dp" android:textSize="18sp" />

    <com.nkdroid.customviews.RobotoLightItalicButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="ROBOTO LIGHT ITALIC" android:padding="8dp" android:textSize="18sp" />

You can make more custom classes just by extending AppCompat views like AppCompatRatingBar, AppCompatSpinner, AppCompatCheckBox, etc

Hope you liked our “custom font in android” tutorial, if you face any truble to implement this tutorial then ask me via comment here, glad to solve your issue.

nirav kalola