Skip to content
Related Articles

Related Articles

How to Change Typeface of TextView in Android?

Improve Article
Save Article
Like Article
  • Last Updated : 27 Sep, 2021

A typeface is a particular design for alphabets that separates it from other typefaces in terms of style, size, and weight variations. In general, there are a lot of local typefaces available on your device or software for use. However, many more typefaces are available on the Internet that can be downloaded and used for respective works.

Similarly, such typefaces can be introduced for displaying the text inside the TextView. So in this article, we will show you how you could use a download typeface and apply it to the text inside the TextView of your Android application.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView in the layout file.

XML




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!--TextView to display the text-->
    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello Geek!"
        android:textSize="40sp"/>
  
</RelativeLayout>

Step 3: Download and store the desired font in the assets folder

We download a font from here. However, you can download a font of your choice. Now, just copy the downloaded font file and paste it into the assets folder. In case, you have no clue about the assets folder, or if it is missing from your folders, create a new assets folder by following this article on Assets Folder in Android Studio.

Step 4: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        val mTextView = findViewById<TextView>(R.id.text_view)
          
        // Creating a typeface
        val font = Typeface.createFromAsset(assets, "JellyBomb.ttf")
  
        // Setting the TextView typeface
        mTextView.typeface = font
    }
}

Output:

You can see that the typeface is applied to the text in the TextView.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!