Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Android – Single TextView with Multiple Colored Text

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will learn about how to add two or more text colors in a single string of TextView of Android using Kotlin. It is very easy to add multiple colors in a single string, just we have to use the Html library and font tag. The syntax is the same as we write in Html for the text that we want to display on the webpage. Here, we don’t have to add two or more Text-View in the activity.xml file. We just need to add a single Text-View so that we can add multiple colors to that TextView. A sample image is given below to get an idea about what we are going to do in this article.

Single TextView with Multiple Colored Text


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. Note that select Kotlin as the programming language.

Step 2: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Comments are added inside the code to understand the code in more detail.


<?xml version="1.0" encoding="utf-8"?>
        app:layout_constraintTop_toTopOf="parent" />

Step 3: Working with the MainActivty.kt file

Add the following code in MainActivty.kt file. Here we will add Html.fromHtml() function and in this function, we will add the font tag and string.

The Tags in Html language are written in the following format.

1. p tag is used for writing the paragraph in webpage.

  • Syntax: <p>string to display</p>
  • E.g. <p>Geeks for Geeks</p>

2. h1, h2 and so on tag is used for writing text in webpage.

  • Syntax: <h1>string to display</h1>
  • E.g. <h1>Lets learn Android</h1>


package com.example.gfg
import android.os.Bundle
import android.text.Html
import android.widget.TextView
class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        textView.text=Html.fromHtml("<font color=${Color.GREEN}>Hello </font>" +
                "<font color=${Color.BLACK}> World!!</font>")


package com.example.gfg;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState)
        TextView textView = findViewById(;
        String html = "<font color=" + Color.GREEN
                      + ">Hello </font><font color="
                      + Color.BLACK + "> World!!</font>";


Android - Single TextView with Multiple Colored Text Output


My Personal Notes arrow_drop_up
Last Updated : 11 Dec, 2022
Like Article
Save Article
Similar Reads