Working with text in Android applications requires not only functionality, but also correct visual design. Bold font is one of the key tools for focusing the user's attention on important interface elements: headings, buttons, warnings or highlighted fragments. However, this can be implemented in several ways - from a simple XML style to programmatic control via Android Studio This can be implemented in several ways - from a simple XML style to programmatic control via Kotlin or Java.

In this article we will analyze all the current methods, including the nuances of working with TextView, SpannableString, styles Material Design and even formatting in strings.xml. We will pay special attention to cross-platform solutions (for example, for Jetpack Compose) and typical errors that lead to incorrect display of text on different versions Android. If you are looking for a way to highlight text in code or layout, here you will find ready-made solutions with examples.

1. Making text bold in XML markup

The simplest and most common way is to use the attribute android:textStyle in the markup file (activity_main.xml or fragment_layout.xml). This method is suitable for static text that does not require dynamic changes at runtime.

Basic syntax example:

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="This text is bold"

android:textStyle="bold" />

To combine styles (for example, bold and italic), use the symbol |:

android:textStyle="bold|italic"
  • โœ… Pros: simplicity, not requires code, supported by all versions Android.
  • โŒ Cons: cannot be applied to part of the text within one TextView.
โš ๏ธ Attention: If you use custom fonts via android:fontFamily, make sure that the selected font supports bold style. Otherwise, the attribute textStyle="bold" will not have an effect.
๐Ÿ“Š Which method of text selection do you use more often?
XML markup
Programmatically in Kotlin/Java
SpannableString
Styles in themes.xml
Other

2. Dynamically changing text style in Kotlin/Java

When text or its formatting changes while the application is running (for example, when a button is clicked), you need to control the style programmatically. In Kotlin i Java this is done through the method setTypeface() with flag Typeface.BOLD.

Example on Kotlin:

val textView = findViewById<TextView>(R.id.my_text_view)

textView.setTypeface(null, Typeface.BOLD)

For Java the syntax is similar:

TextView textView = findViewById(R.id.my_text_view);

textView.setTypeface(null, Typeface.BOLD);

  • ๐Ÿ”น Tip 1: The first parameter setTypeface(null, ...) resets the font family to the standard one. If you need to save a custom font, specify it explicitly: textView.typeface = ResourcesCompat.getFont(context, R.font.my_font).
  • ๐Ÿ”น Tip 2: To dynamically switch between bold and regular style, save the original Typeface into a variable.

Make sure that the TextView is initialized (not null)

Save the original Typeface if a rollback is required

Check font support on target versions of Android

Test on an emulator with different screen sizes-->

3. Partial text selection using SpannableString

If you need to highlight only part of the text within one TextView (for example, one word in a sentence), use the class SpannableString together with StyleSpan. This method is indispensable for working with multi-line text or formatting individual characters.

An example of highlighting the word "important" in the text:

val text = "This message is very important for the user"

val spannableString = SpannableString(text)

// Selecting the word "important" (starts from the 17th character, length 5)

spannableString.setSpan(

StyleSpan(Typeface.BOLD),

17, // start

22, // end

Spannable.SPAN_EXCLUSIVE_EXCLUSIVE

)

textView.text = spannableString

For Java replacement is minimal - use new SpannableString() i new StyleSpan(Typeface.BOLD).

Method Partial formatting support Dynamic changes Complexity of implementation
android:textStyle="bold" โŒ No โŒ No โญ Very simple
setTypeface(Typeface.BOLD) โŒ No โœ… Yes โญโญ Simple
SpannableString + StyleSpan โœ… Yes โœ… Yes โญโญโญ Medium
HTML markup in strings.xml โœ… Yes โŒ No (static) โญโญ Easy
โš ๏ธ Attention: When using SpannableString on texts with emoji or special characters (for example, hieroglyphs), please count indexes start and end taking into account their length in UTF-16. For precise positioning, use text.length or libraries like ICU4J.

4. Formatting text via HTML in strings.xml

If the text is stored in resources (res/values/strings.xml), you can use HTML tags for formatting. This method is convenient for static texts that do not change at runtime, but require complex design.

Example in strings.xml:

Welcome, %s!

To display it in TextView, use Html.fromHtml():

textView.text = Html.fromHtml(getString(R.string.welcome_message, "Ivan"), Html.FROM_HTML_MODE_LEGACY)
  • ๐Ÿ“Œ Important: Starting from Android N (API 24), method Html.fromHtml() requires a flag FROM_HTML_MODE_LEGACY to support tags like <b>.
  • ๐Ÿ”ง Alternative: For projects on Kotlin you can use the extension String.htmlToSpanned() from libraries Android KTX.
Supported HTML tags in Android

5. Using styles and themes (themes.xml)

To ensure consistency in text design throughout the application, it is recommended to place styles in separate resources. This makes it easy to maintain and change the design without editing each TextView manually.

Step 1. Define the style in res/values/styles.xml:

<style name="BoldText">

<item name="android:textStyle">bold</item>

<item name="android:textSize">16sp</item>

<item name="android:textColor">@color/black</item>

</style>

Step 2. Apply the style to TextView:

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Styled text" />

To dynamically apply a style programmatically:

textView.setTextAppearance(R.style.BoldText)
๐Ÿ’ก

If you use Material Design, pay attention to the style attributes textAppearanceMaterial* (for example, textAppearanceMaterialHeadline1). They automatically adjust the boldness and size of the text to the guidelines Google.

6. Bold text in Jetpack Compose

In modern applications, the Jetpack Compose text highlighting syntax differs from the classic XML. Here we use a modifier fontWeight with value FontWeight.Bold.

Example for the entire text block:

Text(

text = "Bold text in Compose",

fontWeight = FontWeight.Bold

)

For partial selection, use AnnotatedString:

Text(

buildAnnotatedString {

append("Plain text ")

withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {

append("bold fragment")

}

}

)

  • ๐ŸŽจ Bonus: In Compose you can animate the change in text boldness using animate*AsState.
  • ๐Ÿ”„ Migration: If you transfer the project from XML to Compose, replace all SpannableString to AnnotatedString.
๐Ÿ’ก

Jetpack Compose does not support HTML markup in text. For complex formatting, use exclusively AnnotatedString i SpanStyle.

7. Typical errors and their solutions

Even experienced developers encounter problems when working with bold text in Android Studio. Here are the most common cases and ways to fix them:

  • ๐Ÿšซ Problem: textStyle="bold" does not work with a custom font.

    Solution: Use android:textFontWeight="700" (for API 28+) or programmatically use Typeface.create() indicating the path to the bold version of the font.

  • ๐Ÿšซ Problem: SpannableString cuts off the text when changing the language (RTL).

    Solution: Use SpannedString instead SpannableString to support bidirectional text.

  • ๐Ÿšซ Problem: Bold text in Button is not displayed.

    Solution: For buttons, apply the style via android:textAllCaps="false" + style="@style/Widget.AppCompat.Button".

โš ๏ธ Attention: On devices with Android 10 (API 29) and higher, a bug may appear in which dynamically applied Typeface.BOLD is reset after rotating the screen. To avoid this, save the state Typeface to ViewModel or use onSaveInstanceState.

FAQ: Frequently asked questions about bold text in Android

Is it possible to make text bold in EditText?

Yes, but with reservations. For static bold text in EditText use android:textStyle="bold". However, if you need to bold user input text (such as hashtags), you will need a handler TextWatcher + SpannableString.

Example:

editText.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {

val hashtagPattern = Regex("#\\w+")

s?.let {

val spans = it.getSpans(0, it.length, StyleSpan::class.java)

spans.forEach { span -> it.removeSpan(span) }

hashtagPattern.findAll(it).forEach { match ->

it.setSpan(

StyleSpan(Typeface.BOLD),

match.range.first,

match.range.last + 1,

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE

)

}

}

}

// ... other TextWatcher methods

})

Why does bold text look blurry on some devices?

It has to do with rendering fonts on screens with high pixel density (xhdpi, xxhdpi). Solutions:

  1. Use vector fonts (.ttf to res/font) instead of system ones.
  2. Add android:paintFlags="antiAlias" to TextView.
  3. For Jetpack Compose set textStyle = LocalTextStyle.current.copy(fontWeight = FontWeight.Bold, fontScale = 1f).
How to make text bold in Notification?

In notifications (Notification), bold text is configured via NotificationCompat.Builder i SpannableString:

val title = SpannableString("New message")

title.setSpan(StyleSpan(Typeface.BOLD), 0, title.length, 0)

val builder = NotificationCompat.Builder(context, CHANNEL_ID)

.setContentTitle(title)

.setContentText("Notification text")

.setSmallIcon(R.drawable.ic_notification)

For Android 8.0+ you can also use NotificationCompat.MessagingStyle with bold headings.

Does Android support bold text in WebView?

Yes, but only through HTML/CSS. Example:

webView.loadData(

"Plain text bold text",

"text/html",

"UTF-8"

)

To dynamically change the style, use webView.evaluateJavascript().

How to test bold text on different versions of Android?

Use the following approaches:

  • ๐Ÿ“ฑ Emulators: Create an AVD for API 21, API 28 i API 33 (minimal, popular and latest versions).
  • ๐Ÿงช Instrumental tests: Check the display via Espresso with matchers withText() i withTypeface().
  • ๐ŸŒ Localization: Test texts in Arabic, Chinese and RTL languages (Hebrew) - bold typeface can break alignment.