Android TextView – Italic Text

TextView Italic Text – To set text style of TextView to italic, you can assign textStyle attribute with “italic” 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 Kotlin line approach to change the text style of TextView to ITALIC.

Change Text Style of TextView to ITALIC in XML Layout File

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

The syntax to use textStyle attribute is

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

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="italic"
        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

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 - Italic

Programmatically Change Text Style of TextView to Italic

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.ITALIC 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.ITALIC)

Let us create an Android application with Kotlin support in Android Studio and change the text style of TextView to italic, 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.ITALIC)
    }
}

Conclusion

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