Android TextView – Bold Text

TextView Bold Text – To set text style of TextView to assuming, you can assign textStyle attribute with "bold" in XML layout file or change the text way 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: "assuming", "italic" or "normal". To change the fashion to bold, you have to assign textStyle with "bold".

The syntax to use textStyle aspect is

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

Instance Android Application – Bold Text in TextView

Let united states of america create an Android application with Kotlin back up in Android Studio and change the text manner of TextView in XML layout file.

activity_main.xml

<?xml version="ane.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="middle"         android:textSize="40dp"         android:padding="50dp"         android:textStyle="bold"         android:text="@string/how-do-you-do" /> </LinearLayout>

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

<resource>     <string name="app_name">TextView Tutorial - TutorialKart</string>     <cord proper name="hello">Howdy World!</string> </resource>

There is no need to change the MainActivity.kt file. The default lawmaking 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: Package?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)     } }

Run this application, and yous would go the following screenshot.

Android TextView Bold

Programmatically Change Text Style of TextView to Assuming

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

To prepare the text style of TextView widget, telephone call setTypeface() method on the TextView widget reference and pass Typeface.Assuming as its second argument. You tin can laissez passer null for the first argument.

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

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

Let us create an Android application with Kotlin support in Android Studio and change the text manner 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

<resource>     <string proper name="app_name">TextView Tutorial - TutorialKart</string>     <string proper name="hello">How-do-you-do Earth!</cord> </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.bone.Packet import android.widget.TextView  class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Package?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)          var textView = findViewById<TextView>(R.id.text_view_id)         textView.setTypeface(null, Typeface.BOLD)     } }

Decision

In this Kotlin Android Tutorial, nosotros learned how to set or change the text/font manner of TextView widget to Assuming in Android awarding.