How To Add Custom Font To Android Textview

Introduction

 
Android is one of the most popular operating systems for mobile. In this article, I will show you how to add custom fonts to Android textview using Android Studio.
 
Requirements
Steps should be followed
 
These steps to add custom fonts to Android textview using Android Studio and I have included the source code below.
 
Step 1
 
Open Android Studio and Start a New Android Studio Project.
 
Android
 
Step 2
 
You can choose your application name and choose where your project is stored on the location. If you wish to use C++ for coding the project, mark the "Include C++ support", and click the "Next" button.
 
Android
 
Now, select the version of Android and select the target Android devices.
 
Android
 
Step 3
 
Now, add the activity and click the "Next" button.
 
Android
 
Add Activity name and click "Finish".
 
Android
 
Step 4
 
Go to activity_main.xml, This XML file contains the designing code for an Android app in the activity_main.xml;
 
Android
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="abu.customfont.MainActivity">  
  8.   
  9.     <TextView  
  10.         android:id="@+id/textView"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:text="Hello World!"  
  14.         android:textSize="60sp"  
  15.         android:textColor="@android:color/black"  
  16.         android:layout_centerVertical="true"  
  17.         android:layout_centerHorizontal="true"  
  18.         tools:layout_editor_absoluteY="187dp"  
  19.         tools:layout_editor_absoluteX="31dp" />  
  20. </android.support.constraint.ConstraintLayout>  
Step 5
 
Go to the app right-click and “Directory path” will appear, then click on the path and add a font file in the main folder. The below template shows how to add a font file. I have attached a font file.
 
Android
 
Android
 
Step 6
 
Go to Main Activity.java, This Java program is the back-end language for Android. Add the following code.
  1. package abu.customfont;  
  2.   
  3. import android.graphics.Typeface;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.widget.TextView;  
  7.   
  8. public class MainActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         TextView textView = (TextView) findViewById(R.id.textView);  
  15.         Typeface font = Typeface.createFromAsset(getAssets(), "fonts/FontName.ttf");  
  16.         textView.setTypeface(font);  
  17.   
  18.   
  19.     }  
  20. }  
Step 7
 
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
 
Android
 
Step 8
 
Then, click the Run button or press shift+f10 to run the project. Select the "virtual machine" option and click OK.
 
Android
 

Conclusion

 
We have successfully added the custom font to Android textview using Android Studio.
 
Android


Similar Articles