TextView
bold text
Android development
text formatting
coding tutorial

how to make a specific text on TextView BOLD

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

If you want only part of a TextView to be bold, do not set the whole view’s typeface to bold. Instead, use a Spannable so the bold style applies only to the selected character range.

Use SpannableString for Partial Bold Text

SpannableString lets you attach formatting spans to only part of the text.

kotlin
1import android.graphics.Typeface
2import android.text.SpannableString
3import android.text.Spanned
4import android.text.style.StyleSpan
5
6val text = "Total: 42 items"
7val spannable = SpannableString(text)
8spannable.setSpan(
9    StyleSpan(Typeface.BOLD),
10    7,
11    9,
12    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
13)
14textView.text = spannable

In that example, only 42 becomes bold.

Compute the Range Dynamically

Hard-coded indexes are fine for short examples, but real code should usually find the target substring first.

kotlin
1val text = "Welcome, Alice"
2val target = "Alice"
3val start = text.indexOf(target)
4
5if (start >= 0) {
6    val spannable = SpannableString(text)
7    spannable.setSpan(
8        StyleSpan(Typeface.BOLD),
9        start,
10        start + target.length,
11        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
12    )
13    textView.text = spannable
14}

This is safer when the content changes or is localized.

Use SpannableStringBuilder for Multiple Styles

If you need bold plus color, size, or multiple formatted segments, SpannableStringBuilder is usually more convenient.

kotlin
1import android.graphics.Color
2import android.text.SpannableStringBuilder
3import android.text.Spanned
4import android.text.style.ForegroundColorSpan
5import android.text.style.StyleSpan
6
7val builder = SpannableStringBuilder("Status: Failed")
8builder.setSpan(StyleSpan(Typeface.BOLD), 8, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
9builder.setSpan(ForegroundColorSpan(Color.RED), 8, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
10textView.text = builder

That keeps all formatting in one text object.

XML Works Only for the Whole View

If you set android:textStyle="bold" in XML, the entire TextView becomes bold.

xml
1<TextView
2    android:layout_width="wrap_content"
3    android:layout_height="wrap_content"
4    android:text="Entire line bold"
5    android:textStyle="bold" />

That is correct for whole-view emphasis, but it does not solve partial bolding.

HTML Is Possible but Less Precise

Android can also parse small HTML fragments such as "Hello <b>world</b>", but Spannable is usually the better tool for app-controlled formatting because it is explicit and easier to compose programmatically.

When the text already comes from trusted HTML content, Html.fromHtml(...) can still be useful. For application-generated text, spans are usually cleaner.

Style the Text Close to Where It Is Built

When the bold segment depends on runtime values, build the Spannable in the same place where the final display string is assembled. That keeps the text content and the formatting range logic synchronized, which is especially helpful once localization or pluralization enters the picture.

It is much easier to maintain one formatting step than several disconnected text mutations.

Reuse a Helper for Repeated Formatting

If your app bolds many different labels the same way, wrap the span logic in a small helper instead of duplicating index calculations. That keeps text formatting consistent and reduces off-by-one mistakes in UI code.

Use Resources When the Whole Phrase Is Known

If the bolded phrase is static and fully known ahead of time, consider keeping the full text in a resource and applying spans after loading it. That preserves localization workflow while still letting the app choose which segment to emphasize at runtime.

Common Pitfalls

The biggest mistake is setting the TextView typeface to bold and expecting only one word to change.

Another issue is hard-coding substring indexes in text that may change with localization or formatting updates.

A third problem is using HTML parsing when the text is generated locally and spans would be simpler and more maintainable.

Summary

  • Use SpannableString or SpannableStringBuilder to bold only part of a TextView.
  • Apply StyleSpan(Typeface.BOLD) to the target character range.
  • Prefer dynamic substring lookup over hard-coded indexes when text can change.
  • Use XML textStyle only when the whole view should be bold.
  • Prefer spans over HTML parsing for app-generated formatted text.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.