Skip to content
Related Articles

Related Articles

Text Styles in Android

Improve Article
Save Article
Like Article
  • Last Updated : 29 Jul, 2021

TextView displays the declared text in a standard way unless the developer specifies particular attributes to make the text appear better. These attributes can directly be declared into the TextView tags. However, in order to maintain a scalable and clean code, industrial practices suggest gathering together all such attributes. Meaning, there can be the same or different styles for different UI elements within the application and all of them need to be grouped into a single file that could be called wherever required. Through this article, we want to show you how you can create styles and apply them to TextViews. In our demonstration, we dynamically changed the TextView style by tapping on it.

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: Create styles.xml in the res>values folder

To create a styles file, right-click on the value folder inside the res folder, move the cursor to new, and click on Values Resource File.

Now give it the name “styles” and click on OK. You can use any name of your choice. Remember to check if the directory name is values, as our file shall contain attribute values. The generated file will be an XML file.

Once the file is generated, it should appear like this. Now, we can add the styles to this file. Go to the next step to see how you could create a style.

Step 3: Add styles in the styles.xml file

Refer to the below code. We have already declared a few styles for you. It is necessary to give a name to each style. Attributes related to text are declared as items between the style opening and closing tags.


<?xml version="1.0" encoding="utf-8"?>
    <style name="whiteText">
        <item name="android:textStyle">italic</item>
        <item name="android:textColor">#ffffff</item>
    <style name="gfgGreenText">
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">#0f9d58</item>

Step 4: Add colors to change the background of the TextView in the colors.xml file (Optional)

This step is optional. We declared two colors of our interest just to change the TextView background.


<?xml version="1.0" encoding="utf-8"?>
    <!--There could be a list of colors declared here-->
    <!--Additionally, we are adding the below two colors-->
    <color name="gfg_green">#0f9d58</color>
    <color name="white">#FFFFFFFF</color>

Step 5: Add a TextView in the layout file (activity_main.xml)


<?xml version="1.0" encoding="utf-8"?>
        android:textStyle="bold" />

Step 6: Write a program to toggle between the styles on TextView click in the Main code (MainActivity.kt)


import android.os.Build
import android.os.Bundle
import android.widget.TextView
import androidx.annotation.RequiresApi
import org.w3c.dom.Text
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        // Declaring TextView from the layout
        val tv1 = findViewById<TextView>(
        // A simple toggle variable that keeps
        // changing on TextView click/tap
        var toggle = true
        // What happens when the 
        // TextView in clicked/tapped
        tv1.setOnClickListener {
            // If toggle is true, then text will become 
            // white and background will become green
            // Else text is green and background is white
            } else {
            // Logically inversing the toggle, i.e. if toggle
            // is true then it shall become false
            // And vice-versa to keep the styles
            // keep changing on every click/tap
            toggle = !toggle


Keep tapping on the TextView to observe the changes.


We can see that the Text style changes to bold and then Italic along with background color changes. The cycle keeps repeating on every even click/tap.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!