Using External fonts in Android

Using external fonts in Android is very easy. We will see the process step by step.

So let’s get started!!!

In this post we will see how to use different fonts on same TextView on click of a Button

Start a new Project
1.Create a new project. File ⇒ New Project.
2. Fill the required details
3.Create a folder called fonts under assets folder and place all your fonts in it.
Untitled
4.Now in your main.xml file create a TextView and 3 Buttons.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="infiniteloopsinc.androidexternalfonts.MainActivity">

    <TextView android:id="@+id/textView"
 android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true"
 android:layout_centerVertical="true"
 android:text="Hello World!" 
android:textSize="45sp"/>

    <LinearLayout android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:layout_below="@+id/textView" 
android:layout_margin="20dp" 
android:orientation="horizontal" 
android:weightSum="3">

        <Button android:id="@+id/btnGothic" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="Gothic"/>

        <Button android:id="@+id/btnGigi"
 android:layout_width="wrap_content" 
android:layout_height="wrap_content"
 android:layout_weight="1" 
android:text="Gigi"/>

        <Button android:id="@+id/btnLato"
 android:layout_width="wrap_content" 
android:layout_height="wrap_content"
 android:layout_weight="1"
 android:text="Lato"/>
    </LinearLayout>
</RelativeLayout>


 

  1. Now open your MainActivity class file and try following code. In the following code i am importing font from assets folder and using TypeFace class to initialize the custom fonts.
public class MainActivity extends AppCompatActivity {

    Typeface fontGothic, fontGigi, fontLato;
    Button buttonGothic, buttonGigi, buttonLato;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fontGothic = Typeface.createFromAsset(this.getAssets(), "GOTHIC.TTF");
        fontGigi = Typeface.createFromAsset(this.getAssets(), "GIGI.TTF");
        fontLato = Typeface.createFromAsset(this.getAssets(), "LATO.TTF");

        buttonGothic = (Button) findViewById(R.id.btnGothic);
        buttonGigi = (Button) findViewById(R.id.btnGigi);
        buttonLato = (Button) findViewById(R.id.btnLato);

        textView = (TextView) findViewById(R.id.textView);


        buttonGothic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setTypeface(fontGothic);
            }
        });

        buttonGigi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setTypeface(fontGigi);
            }
        });

        buttonLato.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textView.setTypeface(fontLato);
            }
        });

    }
}

6.Initialize Typeface before using it.

fontGothic = Typeface.createFromAsset(this.getAssets(), "GOTHIC.TTF");

Note:Font name is case-sensitive

 

7.Using onClickListener of specific Button we will set the font type

buttonGothic.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        textView.setTypeface(fontGothic);
    }
});
  1. Run the project.


Capture