Selecting bold text in Android applications is one of the basic tasks when working with interfaces. It would seem, what could be simpler? But depending on the technologies used (XML, Jetpack Compose, app code on Kotlin/Java) the approaches are radically different. Moreover, incorrect application of methods can lead to performance problems or incorrect display on different versions. Many novice developers are faced with confusion: where to use Android.
Many novice developers are faced with confusion: where to use android:textStyle="bold"and where Typeface.BOLD? Why is the Jetpack Compose syntax different? And how to make only part of the text in TextViewbold? In this article we will analyze all the current methods with practical examples, nuances and comparative tables. We will pay special attention to typical errors that generate bugs in UI.
If you work with legacy code or support an application on API below 21, some methods may behave unpredictably. Use Html.fromHtml() for bold text on modern versions of Android requires mandatory processing of flags Html.FROM_HTML_MODE_LEGACY, otherwise the text will be displayed without formatting.
โโโ
1. Bold text in XML markup: the simplest way
The most obvious method is to set the style directly in the markup file (activity_main.xml or fragment_*.xml). To do this, use the attribute android:textStyle with value "bold". This approach works for all versions Android and does not require software intervention.
Basic usage example:
<TextViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This text is bold"
android:textStyle="bold"/>
If you need to combine styles (for example, bold and italic), list them separated by a vertical bar:
android:textStyle="bold|italic"
- โ Pros: maximum simplicity, no connection to code, works for everyone API.
- โ ๏ธ Cons: you cannot make only part of the text bold in one
TextView. - ๐ง Nuance: if the text is specified via
android:text, and the style is programmatic, the code will have priority.
โ ๏ธ Attention: When using custom fonts (android:fontFamily), the attributetextStyle="bold"may not work. In this case, a separate font file with bold style is required (for example, Roboto-Bold.ttf).
โโโ
2. Programmatic bolding in Java/Kotlin
When the style needs to be set dynamically (for example, when a button is clicked or after loading data), a programmatic approach is used. In Kotlin and Java there is two main methods: through Typeface or SpannableString.
Method 1: Typeface.BOLD
val textView = findViewById(R.id.my_text_view)
textView.setTypeface(null, Typeface.BOLD)
Here null means that the default font is used. The second parameter is the style (Typeface.NORMAL, Typeface.BOLD, Typeface.ITALIC).
Method 2: SpannableString (for part of the text)
val spannableString = SpannableString("Only this word is bold")spannableString.setSpan(
StyleSpan(Typeface.BOLD),
7, // starting position
12, // ending position
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
textView.text = spannableString
| Method | Partial support formatting | Performance | Minimum API |
|---|---|---|---|
Typeface.BOLD | โ No | โก Fast | 1 |
SpannableString | โ Yes | ๐ข Slower (with large text). data-i="88">or save attributes to variables before changing. | 1 |
Html.fromHtml() | โ Yes | ๐ข Slow (HTML parsing) | 1 |
โ ๏ธ Attention: MethodsetTypeface()resets all previous text styles. If you need to maintain color or size, please applySpannableor save attributes to variables before changing.
โโโ
3. data-i="93">In a modern library
In a modern library Jetpack Compose the syntax is radically different. There is no TextView - instead, a component is used Text with the parameter fontWeight.
Basic example:
Text(text = "Bold text in Compose",
fontWeight = FontWeight.Bold
)
For partial selection, use buildAnnotatedString:
Text(buildAnnotatedString {
append("Plain text")
withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
append("bold fragment")
}
}
)
- ๐จ Advantage: declarative approach, automatic optimization UI.
- ๐ Disadvantage: incompatibility with legacy code on
XML. - ๐ Nuance:
FontWeight.Boldcorresponds to a weight of 700. For bold (600) useFontWeight.SemiBold.
In Jetpack Compose, you can create a custom TextStyle bold font and reuse it throughout the application via MaterialTheme.typography.
โโโ
4. HTML markup for bold text
If the text comes from the server in format HTML (for example, from API or CMS), you can use Html.fromHtml(). This method parses tags <b>, <strong> and converts them to bold text.
Example:
val htmlText = "This is bold text"
textView.text = Html.fromHtml(htmlText, Html.FROM_HTML_MODE_LEGACY)
For API 24+ it is recommended to use Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT), since FROM_HTML_MODE_LEGACY outdated.
โ ๏ธ Warning: Parsing HTML in text can become a vulnerability if the data comes from the user. Always escape untrusted content or use android:textIsSelectable="false" to protect against injections.
Use FROM_HTML_MODE_COMPACT on API 24+
Screen user input
Limit the length of parsed text
Test display on different versions of Android-->
โโโ
5. data-i="130">If the project uses non-standard fonts (for example,
If your project uses non-standard fonts (for example, Google Fonts or downloaded .ttf/.otf), the bold style must be set through a separate font file. In Android Studio this is done like this:
Step 1. Place the font files in folder res/font (for example, roboto_regular.ttf and roboto_bold.ttf).
Step 2. Create a font family in res/font/fonts.xml:
<?xml version="1.0" encoding="utf-8"?><font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font android:fontStyle="normal" android:fontWeight="400" android:font="@font/roboto_regular"/>
<font android:fontStyle="normal" android:fontWeight="700" android:font="@font/roboto_bold"/>
</font-family>
Step 3. Apply to markup or code:
<TextViewandroid:fontFamily="@font/fonts"
android:textStyle="bold"/>
- ๐ Important: font weight (
fontWeight) infonts.xmlmust correspond to the actual weight in the file.ttf. - ๐ Check: if bold text is not used, open the font file in an editor (for example, FontForge) and make sure that it supports the style
Bold.
What to do if the custom font is not displayed in bold?
1. Check that the font file actually contains a bold style (not all free fonts support Bold).
2. Make sure that fonts.xml is correct fontWeight (700 for Bold).
3. Reload the project in Android Studio (sometimes the font cache gets lost).
4. Check whether the style is overridden in the theme or parent container.
โโโ
6. Dynamically changing bold text by condition
A common task is to change the text style depending on the state of the application (for example, bold text for an active menu item). via Data Binding or directly in the code.
Example with Data Binding:
<data><variable name="isActive" type="boolean"/>
</data>
<TextView
android:text="@{item.text}"
android:textStyle="@{isActive ? `bold` : `normal`}"/>
Example with Kotlin (without Binding):
fun updateTextStyle(isActive: Boolean) {textView.setTypeface(
null,
if (isActive) Typeface.BOLD else Typeface.NORMAL
)
}
For Jetpack Compose the logic is even simpler:
Text(text = "Menu item",
fontWeight = if (isSelected) FontWeight.Bold else FontWeight.Normal
)
โ ๏ธ Attention: Frequently changing the text style in a loop (for example, inRecyclerView) can cause lags. Optimize the code usingDiffUtilor cache objectsTypeface.
โโโ
7. Common mistakes and how to avoid them
Even experienced developers sometimes encounter problems when working with bold text. Here are the most common errors and their solutions:
- ๐ซ Error: Bold text is not applied in
XML.
Solution: Check whether the style is overridden in the theme. (styles.xml) or parentTextView. - ๐ซ Error:
SpannableStringtruncates the text.
Solution: Make sure that the indexes insetSpan()do not exceed line boundaries (usetext.length). - ๐ซ Error: T Jetpack Compose The text becomes bold only after rebuilding.
Solution: Wrap the variable text inrememberor usemutableStateOf. - ๐ซ Error:
Html.fromHtml()ignores tags<b>.
Solution: Add a flagFROM_HTML_MODE_LEGACY(orFROM_HTML_MODE_COMPACTfor new ones API).
| Symptom | Probable cause | Solution |
|---|---|---|
| Bold text is displayed as normal | No support Bold in a custom font | Download the correct font file or use the system one |
| Text becomes bold with a delay | Long parsing HTML or heavy Span-objects | Move the operation to a background thread or optimize the markup |
| Bold style is reset when scrolling | Wrong implementation RecyclerView.Adapter | Use DiffUtil and cache Typeface |
Always test bold text on devices with different versions of Android and screen density (mdpi, hdpi, xhdpi). On some devices, system fonts may display Bold incorrectly.
โโโ
FAQ: Frequently asked questions about bold text in Android
Is it possible to make text in Button bold?
Yes, Button is inherited from TextView, so all methods (XML, Typeface, Spannable) work similarly. Example:
<Buttonandroid:text="Bold button"
android:textStyle="bold"/>
To partially highlight the text in a button, use SpannableString.
Why doesn't textStyle="bold" work with a custom font?
Most custom fonts do not have built-in bold. you need to:
- Find or generate
Boldversion of the font (for example, via Font Squirrel). - Add it to the folder
res/font. - Specify in
fonts.xmlcorrectfontWeight(700 forBold).
If the font does not support Bold, you can imitate it through Paint.setFakeBoldText(true), but this degrades the rendering quality.
How to make text bold in EditText?
EditText is a successor TextView, so all methods are applicable:
<EditTextandroid:hint="Bold hint"
android:textStyle="bold"/>
For dynamic changes, use:
editText.setTypeface(null, Typeface.BOLD)
Please note: bold font in EditText may make it difficult to read user input. Test UX on real devices.
How to animate the transition from regular text to bold?
There is no built-in animation for the change Android no built-in animation for changing Typeface, but you can use:
- Crossfade: Smooth disappearance of one
TextViewand the appearance of another with bold text. - ValueAnimator: Animate property
paint.textSkewX(tilt effect as imitation of boldness). - Lottie: Use JSON animation for text (for example, via Airbnb Lottie).
Example with ValueAnimator:
val animator = ValueAnimator.ofFloat(0f, 0.3f).apply {addUpdateListener { textView.paint.textSkewX = it.animatedValue as Float }
duration = 300
}
animator.start()
How to check if a font supports bold?
Open the font file (.ttf/.otf) in one of these tools:
- FontForge (free, cross-platform).
- Glyphs (paid, for macOS).
- Online services like FontDrop or Transfonter.
Look for parameters in the font properties Weight or Style. For Bold weight should be 700.