Open In App

How to add Custom Fonts in Android

Last Updated : 04 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Google Fonts provide a wide variety of fonts that can be used to style the text in Android Studio. Appropriate fonts do not just enhance the user interface but they also signify and emphasize the purpose of the text. There are majorly 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 quite direct and easy. Follow the entire article 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. You may find the downloadable fonts here. Here Dancing Script 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. add-custom-font
  • Step 2: Create the layout in the XML files.
  • Step 3: Now in the MainActivity(necessarily the Activity corresponding to the layout file where the TextView to be customised lies), set the typeface for that TextView. 
activity_main.xml

 

MainActivity.java

 
  • Output: output-ui

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. The picture on the left shows how to add the assets folder to the project whereas the picture on the right shows the added ‘ttf’ file to it. custom-font
  • Step 2: While we keep the XML layout to be same as earlier, the Java code of the MainActivity is modified this way. 
MainActivity.java

 
  • Output: output-ui

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 CalligraffittiRegular.
  • 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. 
CalligraffittiRegular.java

 
activity_main.xml

 

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.

activity_main.xml

 

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads

How to add a custom styled Toast in Android
How to Add Custom Switch using IconSwitch Library in Android?
How to add Custom Spinner in android?
How to add a custom styled Toast in Android using Kotlin
How to Add Custom Marker to Google Maps in Android?
MapmyIndia Android SDK - Add Custom Marker
Flutter - Using Google fonts
How to Apply Fonts to the Contents of a Cell Using Java?
How to Install Fonts on Mac?
Fonts Available in Java AWT
Article Tags :
Practice Tags :