how to make a specific text on TextView BOLD
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.
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.
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.
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.
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
SpannableStringorSpannableStringBuilderto bold only part of aTextView. - Apply
StyleSpan(Typeface.BOLD)to the target character range. - Prefer dynamic substring lookup over hard-coded indexes when text can change.
- Use XML
textStyleonly when the whole view should be bold. - Prefer spans over HTML parsing for app-generated formatted text.

