TextView
multi-color text
Android development
UI design
text formatting

Single TextView with multiple colored text

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

A single Android TextView can display multiple colors by applying spans to different character ranges. The standard tool for this is SpannableString or SpannableStringBuilder. The key idea is simple: the TextView still shows one string, but parts of that string carry different formatting metadata.

Use SpannableStringBuilder For Mixed Styling

If you want multiple color segments in one view, SpannableStringBuilder is usually the most convenient choice because it lets you build the text and style ranges in one place.

kotlin
1import android.graphics.Color
2import android.os.Bundle
3import android.text.SpannableStringBuilder
4import android.text.Spanned
5import android.text.style.ForegroundColorSpan
6import android.widget.TextView
7import androidx.appcompat.app.AppCompatActivity
8
9class MainActivity : AppCompatActivity() {
10    override fun onCreate(savedInstanceState: Bundle?) {
11        super.onCreate(savedInstanceState)
12
13        val textView = TextView(this)
14
15        val text = SpannableStringBuilder()
16        text.append("Status: ")
17
18        val start = text.length
19        text.append("Connected")
20        val end = text.length
21
22        text.setSpan(
23            ForegroundColorSpan(Color.parseColor("#2E7D32")),
24            start,
25            end,
26            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
27        )
28
29        textView.text = text
30        setContentView(textView)
31    }
32}

This produces one TextView where Status: keeps the default color and Connected is green.

Coloring Multiple Segments

You can style more than one range by applying more than one span.

kotlin
1val text = SpannableStringBuilder("Error: file not found")
2
3text.setSpan(
4    ForegroundColorSpan(Color.RED),
5    0,
6    5,
7    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
8)
9
10text.setSpan(
11    ForegroundColorSpan(Color.DKGRAY),
12    7,
13    text.length,
14    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
15)
16
17textView.text = text

This is usually better than splitting the sentence across multiple TextView widgets unless the layout genuinely needs separate views.

Prefer Resource Colors Over Hardcoded Values

In real applications, use theme or resource colors instead of hardcoded hex strings where possible.

kotlin
1import androidx.core.content.ContextCompat
2
3val successColor = ContextCompat.getColor(this, android.R.color.holo_green_dark)
4val warningColor = ContextCompat.getColor(this, android.R.color.holo_orange_dark)

Then pass those values into ForegroundColorSpan. This keeps the UI easier to maintain and align with the rest of the design system.

Mixing Color With Other Styles

Spans are composable. You can combine a color span with bold, clickable, or size spans on the same text.

kotlin
1import android.graphics.Typeface
2import android.text.style.StyleSpan
3
4val text = SpannableStringBuilder("Price: $19.99")
5val amountStart = text.indexOf("$")
6
7text.setSpan(
8    ForegroundColorSpan(Color.parseColor("#1565C0")),
9    amountStart,
10    text.length,
11    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
12)
13
14text.setSpan(
15    StyleSpan(Typeface.BOLD),
16    amountStart,
17    text.length,
18    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
19)

This lets one TextView present structured information without fragmenting the layout.

XML HTML-Style Text Is Limited

You may see examples using HTML formatting through Html.fromHtml. That can work for simple cases, but spans are usually the better option because:

  • the range positions are explicit
  • the result is easier to update dynamically
  • you are not depending on limited HTML support in text rendering

Use HTML only when the content already arrives as HTML or when the formatting requirements are very simple.

A Good Helper Function

If your app styles status text repeatedly, wrap the span logic in a helper instead of copying index calculations everywhere.

kotlin
1fun colorRange(text: SpannableStringBuilder, start: Int, end: Int, color: Int) {
2    text.setSpan(
3        ForegroundColorSpan(color),
4        start,
5        end,
6        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
7    )
8}

That keeps the UI code readable and reduces off-by-one mistakes.

Common Pitfalls

  • Using multiple TextView widgets for text that should really be one formatted sentence.
  • Hardcoding character indexes without checking that the source text can change.
  • Using HTML formatting when dynamic spans would be simpler and safer.
  • Choosing colors with poor contrast, which hurts readability and accessibility.
  • Rebuilding large spanned texts repeatedly in tight UI update loops without reason.

Summary

  • Use SpannableString or SpannableStringBuilder to show multiple colors in one TextView.
  • Apply ForegroundColorSpan to the exact character ranges you want to style.
  • Prefer resource or theme colors over hardcoded values in production code.
  • Combine spans when you need color plus other formatting such as bold or size.
  • Keep range calculations centralized if the same formatting pattern appears in multiple places.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms