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? madhavmaheshwarimm20 Follow Improve Article Tags : Java Android DSA Android-Animation Practice Tags : Java Similar Reads DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s 10 min read Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per 15+ min read Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it, 13 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Like