Android: Custom Font in Kotlin

Sometimes you may want to use custom font in your Android application for aesthetic requirement or to show a message in a different language. I have already written an article on how to use a Custom Font in Android four years ago. Since it has been a long time and a reader wondered if that code still works, I am writing this new article using Kotlin. However, the underlying technique hasn't change over the years and you can still use my previous article.

Step 1:
Create an Android application "Custom Font" with Kotlin support.

Step 2:
Right click on the app folder and create a NewFolderAssets Folder. In the appeared dialog, do not change any default values. Just click Next and create an assets folder.

Android: Custom Font in Kotlin

Step 3:
Copy and paste a true-type font file into the assets folder. In this tutorial, a Tamil font baamini.ttf is used.

Step 4:
Modify the activity_main.xml file as shown below. As you can see, I have added two additional properties to the default TextView to set the id and the textAppearance style.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/txtMessage"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>


Step 5:
Append the following code into the onCreate method. This code creates a Typeface using the Baamini font and set it to the TextView along with the text.
// Create a TypeFace using the font file
val typeface = Typeface.createFromAsset(assets, "baamini.ttf")

txtMessage.text = "tzf;fk;"   // Welcome in Tamil
// Set the typeface
txtMessage.typeface = typeface
After the modification, the MainActivity.kt file should look like this:
package com.javahelps.customfont

import android.graphics.Typeface
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Create a TypeFace using the font file
        val typeface = Typeface.createFromAsset(assets, "baamini.ttf")

        txtMessage.text = "tzf;fk;"   // Welcome in Tamil
        // Set the typeface
        txtMessage.typeface = typeface
    }
}
Please compare your project with the following screenshot and make sure that you have made all the changes.

Android: Custom Font in Kotlin

Step 6:
Run your project and check the result.

Android: Custom Font in Kotlin

Find the project @ Git Hub.
Previous
Next Post »

Contact Form

Name

Email *

Message *