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.
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.
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.
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.
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.
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.
That keeps the UI code readable and reduces off-by-one mistakes.
Common Pitfalls
- Using multiple
TextViewwidgets 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
SpannableStringorSpannableStringBuilderto show multiple colors in oneTextView. - Apply
ForegroundColorSpanto 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.
Related reading
- Singletons vs. Application Context in Android?
- Singletons vs. Application Context in Android?
- Sizing a Container View with a controller of dynamic size inside a scrollview
- Slow app launch time after updating to iOS 14 and Xcode 12
- Snap to center of a cell when scrolling UICollectionView horizontally
- Solution to INSTALL_FAILED_INSUFFICIENT_STORAGE error on Android
- SourceKitService Consumes CPU and Grinds Xcode to a Halt
- SourceKitService Consumes CPU and Grinds Xcode to a Halt
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseData 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.