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:

<TextView

android: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 attribute textStyle="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

MethodPartial support formattingPerformanceMinimum API
Typeface.BOLDโŒ Noโšก Fast1
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: Method setTypeface() resets all previous text styles. If you need to maintain color or size, please apply Spannable or save attributes to variables before changing.
๐Ÿ“Š Which method do you use more often?
XML markup
Typeface in code
SpannableString
Html.fromHtml()

โ•โ•โ•

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.Bold corresponds to a weight of 700. For bold (600) use FontWeight.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:

<TextView

android:fontFamily="@font/fonts"

android:textStyle="bold"/>

  • ๐Ÿ“Œ Important: font weight (fontWeight) in fonts.xml must 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, in RecyclerView) can cause lags. Optimize the code using DiffUtil or cache objects Typeface.

โ•โ•โ•

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 parent TextView.
  • ๐Ÿšซ Error: SpannableString truncates the text.
    Solution: Make sure that the indexes in setSpan() do not exceed line boundaries (use text.length).
  • ๐Ÿšซ Error: T Jetpack Compose The text becomes bold only after rebuilding.
    Solution: Wrap the variable text in remember or use mutableStateOf.
  • ๐Ÿšซ Error: Html.fromHtml() ignores tags <b>.
    Solution: Add a flag FROM_HTML_MODE_LEGACY (or FROM_HTML_MODE_COMPACT for new ones API).
SymptomProbable causeSolution
Bold text is displayed as normalNo support Bold in a custom fontDownload the correct font file or use the system one
Text becomes bold with a delayLong parsing HTML or heavy Span-objectsMove the operation to a background thread or optimize the markup
Bold style is reset when scrollingWrong implementation RecyclerView.AdapterUse 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:

<Button

android: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:

  1. Find or generate Boldversion of the font (for example, via Font Squirrel).
  2. Add it to the folder res/font.
  3. Specify in fonts.xml correct fontWeight (700 for Bold).

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:

<EditText

android: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:

  1. Crossfade: Smooth disappearance of one TextView and the appearance of another with bold text.
  2. ValueAnimator: Animate property paint.textSkewX (tilt effect as imitation of boldness).
  3. 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.