Open In App

Android – Single TextView with Multiple Colored Text

Last Updated : 11 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




<!--activity_main.xml-->
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
 
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="55sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
 
</androidx.constraintlayout.widget.ConstraintLayout>


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>

Kotlin




package com.example.gfg
 
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Html
import android.widget.TextView
 
@Suppress("DEPRECATION")
class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        textView=findViewById(R.id.textview)
        textView.text=Html.fromHtml("<font color=${Color.GREEN}>Hello </font>" +
                "<font color=${Color.BLACK}> World!!</font>")
    }
}


Java




package com.example.gfg;
 
import android.graphics.Color;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView textView = findViewById(R.id.textview);
        String html = "<font color=" + Color.GREEN
                      + ">Hello </font><font color="
                      + Color.BLACK + "> World!!</font>";
        textView.setText(Html.fromHtml(html));
    }
}


Output:

Android - Single TextView with Multiple Colored Text Output

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads