ZOFTINO.COM android and web dev tutorials

Android Using Custom Fonts

In your android application, if you want to use external or custom fonts which are not supported by android platform, you can do so by providing font files, configuring fonts and applying custom font family to text widgets in the application.

Android 8.0 (API 26) introduced a new feature, fonts in xml, using which you can use fonts as resources. One more important feature related to fonts introduced in Android 8.0 (API 26) is that font files can be downloaded from font authority. This feature of downloadable fonts reduces your app size as you don’t need to include font files in your app. You can refer android downloadable fonts tutorial for more information about configuring and using downloadable fonts.

In this tutorial, you can learn using custom fonts in android application by including font file in the application and using Android 8.0 (API 26) fonts in xml feature.

For backward compatibility, you can use android support library version 26. Font as xml resource feature works on devices with android api version 14 and higher.

Below are the steps to configure custom fonts in android.

Add Support Library 26 to Project

Add below entry to module gradle build file.

dependencies {
    . . . 
    implementation 'com.android.support:support-compat:26.0.1'
}

Custom Font Files

Get font files related to external or custom font that you want to use in your application. Since we are going to use Google fonts in our example android app, I downloaded ubuntu google font files from google fonts.

Font Folder

Next, you need to create font folder under res folder and save custom font files in it. You can refer to fonts in below picture using constants like R.font.ubuntu_regular, R.font. ubuntu_medium and R.font. ubuntu_bold7 and used them in application.

android custom fonts example

Create Font Family

Instead of referring to fonts individually, you can define font family containing fonts of different font files with different weight and style as shown below. You can refer to the fontfamily xml in your application by using resource constants like R.font.filename.

 <?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/ubuntu_regular" />
    <font
        android:fontStyle="normal"
        android:fontWeight="500"
        android:font="@font/ubuntu_medium" />
    <font
        android:fontStyle="normal"
        android:fontWeight="500"
        android:font="@font/ubuntu_bold7" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/ubuntu_italic" />
</font-family>

Using Custom Fonts

You can use custom font on widgets by setting fontfamily attribute of widets or by defining custom style and using it.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android.support.design="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textView2"
        android:fontFamily="@font/ubuntu"
        . . ./>
    <TextView
        android:id="@+id/textView3"
        android:fontFamily="@font/ubuntu"
        android:textStyle="bold"
       . . . />
    <TextView
        android:id="@+id/textView4"
        android:fontFamily="@font/ubuntu"
        android:textStyle="italic"
	. . . />

Output of above layout with custom fonts.

android custom fonts
 <style name="CustomFont" parent="TextAppearance.AppCompat.Title">
    <item name="android:fontFamily">@font/ubuntu</item>
</style>

Using Custom Fonts Programatically

TextView textView = (TextView) findViewById(R.id.textView2);
Typeface typeface = getResources().getFont(R.font.ubuntu);
textView.setTypeface(typeface);