How to Add Custom Fonts in Android Studio?

Custom Fonts in Android Studio

It’s easy to add custom fonts in the Android Studio project. First, visit the Google Fonts website to download fonts. For this article, I’m using Poppins font.

Next, open Android Studio, right-click on the res folder, and select New > Android Resource Directory.

Android studio resource directory

From the “Resource type” menu, select font and click on the OK button.

font resource directory

Now, font folder is added to the res directory. When you download a font, you will get .ttf files. Copy the files and paste them into our new font folder (right-click on the folder and select paste). Now, expand the folder. You may see errors.

custom font errors

This is because the name of the resources should be written in small characters. Also, we should only use the underscore to separate the words. To change their name, right-click on the font and select Refactor > Rename.

rename font resources

Use small letters and add the underscore between the words.

fonts rename

Now, you have successfully added the fonts to your project. Let’s see how to use them in the code.

If you are using XML:

You can use android:fontFamily tag to add fonts:

android:fontFamily="@font/poppins_bold"

Example:

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="SemicolonSpace"
    android:fontFamily="@font/poppins_bold" />

If you are using Jetpack Compose, there are different ways to use custom fonts. Follow this article on Jetpack Compose Themes.

This is how you add custom fonts in Android Studio. If you have any doubts, comment below.

Related:

Leave a Comment