Android has supported setting custom fonts from a long time, but it has always missed out on one feature, setting fonts in xml. It has only allowed using fonts programmatically. Android team has finally resolved this pitfall in Support Library 26. Now you can use fonts in xml, just like any other resource.

Using fonts in XML

First step that you need to do is download some font files. You can get some good looking font files easily, just search on Google or use fonts provided by Google itself.

Now you need to create the resource folder for fonts in your project. Right click on res -> go to New -> click on Android Resource Directory.

Place the downloaded font files(.ttf) into the fonts folder. I have downloaded 3 fonts: Economica, Satisfy and Tangerine.

Now you can refer to your font files directly from your xml. Use the fontFamily property to set the font from the resource directory.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:padding="16dp" tools:context="com.example.gurleensethi.myapplication.FontActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" <!-- Directly access fonts--> android:fontFamily="@font/economica_regular" android:text="Be happy for this moment. This moment is your life." android:textSize="32sp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" <!-- Directly access fonts--> android:fontFamily="@font/satisfy_regular" android:text="The secret to happiness is freedom... And the secret to freedom is courage." android:textSize="40sp" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="I am programmatically set" android:textSize="48sp" /> </LinearLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 <? xml version = "1.0" encoding = "utf-8" ?> < LinearLayout 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" android : orientation = "vertical" android : padding = "16dp" tools : context = "com.example.gurleensethi.myapplication.FontActivity" > < TextView android : layout_width = "wrap_content" android : layout_height = "wrap_content" < ! -- Directly access fonts -- > android : fontFamily = "@font/economica_regular" android : text = "Be happy for this moment. This moment is your life." android : textSize = "32sp" / > < TextView android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "16dp" < ! -- Directly access fonts -- > android : fontFamily = "@font/satisfy_regular" android : text = "The secret to happiness is freedom... And the secret to freedom is courage." android : textSize = "40sp" / > < TextView android : id = "@+id/textView" android : layout_width = "wrap_content" android : layout_height = "wrap_content" android : layout_marginTop = "16dp" android : text = "I am programmatically set" android : textSize = "48sp" / > < / LinearLayout >

Adding fonts Programmatically

You can set the fonts programmatically as well.

//Setting font programmatically val typeface = ResourcesCompat.getFont(this, R.font.tangerine_regular) textView.typeface = typeface 1 2 3 4 //Setting font programmatically val typeface = ResourcesCompat . getFont ( this , R . font . tangerine_regular ) textView . typeface = typeface

We use ResourcesCompact here because we need to make sure that this code runs perfectly on previous versions of Android. If you try to use the getFont method on the resources by using getResources then Studio will give an error as that function can only be used on API 26 or above, so make sure you use ResourcesCompat.

Below is how the layout will look.

Now you can use fonts in your app very easily, but try not to use too much of them, using a mixture of too many font styles can ruin the overall app appearance and specially the UX. Here are the material guidelines when using typography in your app.

Android Developers Documentation

Getting Started with Kotlin