How to add Custom Fonts in Android

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



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      
        <TextView
            android:id="@+id/textview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GeeksforGeeks"
            android:textColor="#006600"
            android:textSize="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
      
    </androidx.constraintlayout.widget.ConstraintLayout>

    chevron_right

    
    

    MainActivity.java

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.android.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.dancing_script_bold);
            textView.setTypeface(typeface);
        }
    }

    chevron_right

    
    

  • 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.android.customfonts;
      
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.content.res.ResourcesCompat;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.widget.TextView;
    import com.example.android.customfonts.R;
      
    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(),
                "macondo_swash_caps_regular.ttf");
            textView.setTypeface(typeface);
        }
    }

    chevron_right

    
    

  • Output:
    output-ui

Method 2

In this method we’ll create a separate java class dediacted 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 constuctors.
  • 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

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    package com.example.android.customfonts;
      
    import android.content.Context;
    import android.graphics.Typeface;
    import android.util.AttributeSet;
      
    public class CalligraffittiRegular extends
             androidx.appcompat.widget.AppCompatTextView {
      
        public CalligraffittiRegular(Context context)
        {
            super(context);
            initTypeface(context);
        }
      
        public CalligraffittiRegular(Context context,
                                     AttributeSet attrs)
        {
            super(context, attrs);
            initTypeface(context);
        }
      
        public CalligraffittiRegular(Context context,
                                     AttributeSet attrs,
                                     int defStyleAttr)
        {
            super(context, attrs, defStyleAttr);
            initTypeface(context);
        }
      
        private void initTypeface(Context context)
        {
            Typeface tf = Typeface.createFromAsset(
                context.getAssets(),
                "calligraffitti_regular.ttf");
            this.setTypeface(tf);
        }
    }

    chevron_right

    
    

  • Step 7: Now in your XML layout file, use this font class instead of of the conventional TextView tag.

    activity_main.xml

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      
        <com.example.android.customfonts.CalligraffittiRegular
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="GeeksforGeeks"
            android:textColor="#006600"
            android:textSize="50dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintHorizontal_bias="0.616"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.462"/>
      
    </androidx.constraintlayout.widget.ConstraintLayout>

    chevron_right

    
    

  • Output:
    output-ui

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.