Android TextView – Bold Text

TextView Bold Text – To set text style of TextView to bold, you can assign textStyle attribute with “bold” in XML layout file or change the text style dynamically in Kotlin file using setTypeface() method.

In this tutorial, we will learn both the layout file approach and programmatical(dynamic) approach to change the text style of TextView to BOLD.

Change Text Style of TextView to BOLD in XML Layout File

textStyle attribute of TextView widget accepts on of these values: "bold", "italic" or "normal". To change the style to bold, you have to assign textStyle with "bold".

The syntax to use textStyle attribute is

<TextView
    ...
    android:textStyle="bold" />
ADVERTISEMENT

Example Android Application – Bold Text in TextView

Let us create an Android application with Kotlin support in Android Studio and change the text style of TextView in XML layout file.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout_id"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:textSize="40dp"
        android:padding="50dp"
        android:textStyle="bold"
        android:text="@string/hello" />
</LinearLayout>

We have used a string resource, and the contents of strings.xml is

<resources>
    <string name="app_name">TextView Tutorial - TutorialKart</string>
    <string name="hello">Hello World!</string>
</resources>

There is no need to change the MainActivity.kt file. The default code would do.

MainActivity.kt file

package com.tutorialkart.textviewtutorial

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

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

Run this application, and you would get the following screenshot.

Android TextView Bold

Programmatically Change Text Style of TextView to Bold

We can get the reference to TextView widget present in layout file and change the text style dynamically with Kotlin code.

To set the text style of TextView widget, call setTypeface() method on the TextView widget reference and pass Typeface.BOLD as its second argument. You can pass null for the first argument.

The syntax to set text style using setTypeface() method is

var textView = findViewById<TextView>(R.id.text_view_id)
textView.setTypeface(null, Typeface.BOLD)

Let us create an Android application with Kotlin support in Android Studio and change the text style of TextView to bold, dynamically/programmatically in Kotlin file.

The layout file contains a TextView. Since we are about to change the text style in Kotlin file, textStyle is not specified in layout XML file.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout_id"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text_view_id"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:gravity="center"
        android:textSize="40dp"
        android:padding="50dp"
        android:text="@string/hello" />
</LinearLayout>

We have used a string resource, and the contents of strings.xml is

<resources>
    <string name="app_name">TextView Tutorial - TutorialKart</string>
    <string name="hello">Hello World!</string>
</resources>

We have got the reference to the TextView in layout file using findViewById() method. Call to setTypeface() method on the TextView reference would set the text color of TextView.

MainActivity.kt

package com.tutorialkart.textviewtutorial

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)

        var textView = findViewById<TextView>(R.id.text_view_id)
        textView.setTypeface(null, Typeface.BOLD)
    }
}

Conclusion

In this Kotlin Android Tutorial, we learned how to set or change the text/font style of TextView widget to Bold in Android application.