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. <TextView
  9. android:id="@+id/textView"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:text="Hello World!"
  13. android:textSize="60sp"
  14. android:textColor="@android:color/black"
  15. android:layout_centerVertical="true"
  16. android:layout_centerHorizontal="true"
  17. tools:layout_editor_absoluteY="187dp"
  18. tools:layout_editor_absoluteX="31dp" />
  19. </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. import android.graphics.Typeface;
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. public class MainActivity extends AppCompatActivity {
  7. @Override
  8. protected void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.activity_main);
  11. TextView textView = (TextView) findViewById(R.id.textView);
  12. Typeface font = Typeface.createFromAsset(getAssets(), "fonts/FontName.ttf");
  13. textView.setTypeface(font);
  14. }
  15. }
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