How to change the Text Color of a Substring in android using SpannableString class? Last Updated : 23 Feb, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will learn about how to change the text color of a substring of a string. It is easy to change the color of the whole string but to change the color of a substring we have to use a special class SpannableString. But SpannableString class is not really helpful when it comes to change the background color of the text. So for that, we have to use SpannableStringBuilder class. Approach: Add the following code in activity_main.xml file. This will add two textviews in the activity_main layout. activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" tools:context=".MainActivity"> <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textAlignment="center" android:text="GeeksForGeeks A Computer Science Portal for Geeks" android:textSize="20sp" /> <TextView android:id="@+id/text_view2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textAlignment="center" android:text="Learn Algorithm." android:textSize="20sp" /> </LinearLayout> Now add the following code in the MainActivity.java file. In this code we will change the color of substrings of first textview with SpannableString class and add a background color in second textview with SpannableStringBuilder. Create the objects of the classes with the texts you want to display and use setSpan function to change the color of the substring. MainActivity.java package org.geeksforgeeks.gfgspannablestring; import android.graphics.Color; import android.os.Bundle; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.style.BackgroundColorSpan; import android.text.style.ForegroundColorSpan; 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.text_view); TextView textView2 = findViewById(R.id.text_view2); String text = "GeeksForGeeks A Computer Science Portal for Geeks"; String text2 = "Learn Algorithm."; SpannableString spannableString = new SpannableString(text); // we can only use backgroundcolor // span with a spannableStringBuilder. SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(text2); // It is used to set foreground color. ForegroundColorSpan green = new ForegroundColorSpan(Color.GREEN); ForegroundColorSpan cyan = new ForegroundColorSpan(Color.CYAN); // It is used to set background color. BackgroundColorSpan yellow = new BackgroundColorSpan(Color.YELLOW); // It is used to set the span to the string spannableString.setSpan(green, 0, 13, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableString.setSpan(cyan, 40, 43, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableStringBuilder.setSpan(yellow, 0, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableString); textView2.setText(spannableStringBuilder); } } Output: Comment More infoAdvertise with us Next Article How to change the Text Color of a Substring in android using SpannableString class? M madhavmaheshwarimm20 Follow Improve Article Tags : Android Android-Animation Similar Reads How to Change the ListView Text Color in Android? In Android, a ListView is a UI element used to display the list of items. This list is vertically scrollable and each item in the ListView is operable. A ListView adapter is used to supply items from the main code to the ListView in real-time. By default, the TextView font size is 14 sp and color is 3 min read How to Change the Color of Status Bar in an Android App? A Status Bar in Android is an eye-catching part of the screen, all of the notification indications, battery life, time, connection strength, and plenty of things are shown here. An Android user may look at a status bar multiple times while using an Android application. It is a very essential part of 4 min read How to Change Text Color of Toolbar Title in an Android App? In an Android app, the toolbar title present at the upper part of the application. Below is a sample image that shows you where the toolbar title is present. In the above image, you may see that the color of the Toolbar Title is white which is by default. So in this article, you will learn how to ch 2 min read How to Change the ProgressBar Color in Android? In this article, we will see how we can add color to a ProgressBar in android. Android ProgressBar is a user interface control that indicates the progress of an operation. For example, downloading a file, uploading a file on the internet we can see the ProgressBar estimate the time remaining in oper 3 min read Android - How to Change the Color of Alternate Rows in RecyclerView RecyclerView is an essential ViewGroup used in Android to display a list of items. It provides a flexible and highly customizable way of implementing a list that can be presented as a grid or a list. In this project, we will be working with RecyclerView to change the color of alternate rows. Typical 10 min read Like