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.
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.
Related reading
- How to make a transparent UIWebView
- How to make a UILabel clickable?
- How to make a UILabel clickable?
- How to make an alert dialog fill 90 of screen size?
- How to make an Android device vibrate? with different frequency?
- How to make an Android device vibrate? with different frequency?
- How to make an Android Spinner with initial text Select One?
- How to make an HTTP request basic auth in Swift
.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 courseTrack 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.