How to add custom Font in Android
Google Fonts provide us a wide variety of fonts that can be used to style the text or textView in Android Studio. The proper fonts do not just enhance the user interface, but they also signify and highlight the purpose of the text. In android, you can define your own custom fonts for the strings in your application.
There are mainly three methods to add custom fonts to text in Android Studio. The first two methods involve the use of the Typeface class while the last method is easy. Follow this tutorial to explore all the methods.
Method 1
In this method, we’ll first download the font’s TTF file from the internet and then use them as an asset or a resource to set the Typeface. Download the font from here. Here Ranchers font is used. Once you download the fonts of your choice, unzip the folder and copy the font file.
By creating a new Android resource directory:
- Step 1: In the project’s resource folder, create a new Android Resource Directory of Resource type: font and paste this ‘ttf’ file here. Note that while pasting it, keep in mind that a resource file’s name can consist of lower-case letters and underscores only, so refactor the file accordingly.
- Step 2: Create the layout in the XML files.
- Step 3: Now in the MainActivity (certainly the Activity corresponding to the layout file where the TextView to be customized lies), set the typeface for that TextView.
activity_main.xml
<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<TextView
android:id=”@+id/textview”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Android Apps\nDevelopment Blogs”
android:textColor=”#F3F3F3"
android:textSize=”40dp”
android:textAlignment=”center”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent”/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.codewithgolap.customfonts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textview);
Typeface typeface = ResourcesCompat.getFont( this, R.font.ranchers_regular);
textView.setTypeface(typeface);
}
}
Output
By creating a new asset folder:
- Step 1: Create a new asset folder (app/New/Folder/Asset folder) in Android Studio and paste the ‘ttf’ file of the font here that you downloaded. The picture on the left side shows how to add the assets folder to the project whereas the picture on the right side shows the added ‘ttf’ file to it.
- Step 2: While we keep the XML layout to be the same as earlier, the Java code of the MainActivity is modified this way.
MainActivity.java
package com.codewithgolap.customfonts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textview);
Typeface typeface = Typeface.createFromAsset( getAssets(), "caveat_variableFont_wght.ttf");
textView.setTypeface(typeface);
}
}
Output
Method 2
In this method, we’ll create a separate java class dedicated to a particular font and use this class instead of the conventional TextView tag in the XML file.
- Step 1: Download the font of your choice and use either of the above two approaches to store it in the project. I have pasted my file in the assets folder.
- Step 2: Create a new Java file in the package. Preferably name it according to the font that you want to implement. Here we have created a file named Maxwell.
- Step 3: Extend the following class in this Java file:
androidx.appcompat.widget.AppCompatTextView
- Step 4: Complete the Java code by adding the required constructors.
- Step 5: Create a method in the class wherein the typeface for the font is set.
- Step 6: Call this method in each constructor. Refer to the following code for a better understanding.
Maxwell.java
package com.codewithgolap.customfonts;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
public class Maxwell extends androidx.appcompat.widget.AppCompatTextView{
public Maxwell(Context context)
{
super(context);
initTypeface(context);
}
public Maxwell(Context context, AttributeSet attrs)
{
super(context, attrs);
initTypeface(context);
}
public Maxwell(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initTypeface(context);
}
private void initTypeface(Context context)
{
Typeface tf = Typeface.createFromAsset( context.getAssets(), "maxwell_bold.ttf");
this.setTypeface(tf);
}
}
- Step 7: Now in your XML layout file, use this font class instead of the conventional TextView tag
activity_main.xml
<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>
<com.codewithgolap.customfonts.Maxwell
android:id=”@+id/textview”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”Android Apps\nDevelopment Blogs”
android:textColor=”#FFFFFF”
android:textSize=”40dp”
android:textAlignment=”center”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent”/>
</androidx.constraintlayout.widget.ConstraintLayout>
Output
Method 3
With Android 8.0 (API Level 26) a simpler method was introduced for using fonts as a resource in Android Studio. The android:fontFamily attribute of the TextView class is used to specify the font that you want to use.
- Step 1: Go to the XML file and go to the Design view.
- Step 2: Click the TextView you want to change the font of.
- Step 3: In the search bar, search for fontFamily.
- Step 4: In the dropdown menu, you can check out the fonts available. In case you want to explore more, scroll down and click ‘More Fonts…‘.
- Step 5: A dialog box pops up. Choose a font of your choice, choose the style you like in the preview, and click OK.
- Step 6: This would create a downloadable font and add it automatically to your project
- The following files automatically get added to your project:
- Step 7: Now the XML file will look like:
activity_main.xml
<?xml version=”1.0" encoding=”utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=”http://schemas.android.com/apk/res/android"
xmlns:app=”http://schemas.android.com/apk/res-auto"
xmlns:tools=”http://schemas.android.com/tools"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><TextView
android:id=”@+id/textview”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:fontFamily=”@font/bangers”
android:text=”Android Apps\nDevelopment Blogs”
android:textAlignment=”center”
android:textColor=”#FFFFFF”
android:textSize=”40dp”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /></androidx.constraintlayout.widget.ConstraintLayout>