Android TextView – Bold Text

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

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

Alter 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 modify the way to bold, y'all take to assign textStyle with "assuming".

The syntax to use textStyle attribute is

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

Instance Android Application – Bold Text in TextView

Let us create an Android awarding with Kotlin support in Android Studio and modify 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="heart"         android:textSize="40dp"         android:padding="50dp"         android:textStyle="assuming"         android:text="@string/hello" /> </LinearLayout>

Nosotros have used a string resources, and the contents of strings.xml is

<resource>     <cord proper name="app_name">TextView Tutorial - TutorialKart</cord>     <string name="hullo">Hello Globe!</string> </resources>

At that place is no need to alter 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  grade MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)     } }

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

Android TextView Bold

Programmatically Alter Text Style of TextView to Bold

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

To set the text mode of TextView widget, phone call setTypeface() method on the TextView widget reference and pass Typeface.Assuming as its 2nd argument. You can pass zero 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(aught, Typeface.Assuming)

Let us create an Android application with Kotlin back up in Android Studio and alter 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-viii"?> <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>

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

<resource>     <string proper noun="app_name">TextView Tutorial - TutorialKart</string>     <cord proper name="hello">Hello Globe!</string> </resources>

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

MainActivity.kt

bundle com.tutorialkart.textviewtutorial  import android.graphics.Typeface import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextView  form 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(cipher, Typeface.BOLD)     } }

Conclusion

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