TextView
textStyle
Android development
Coding tutorial
Bold and Italic text

How to set TextView textStyle such as bold, italic

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Android gives you several ways to style text in a TextView, and the right choice depends on whether the style is static, dynamic, or applied to only part of the text. For simple whole-view styling, XML is enough, but richer formatting usually needs spans or code.

Setting Whole-Text Style in XML

If the text should always be bold, italic, or bold-italic, define it directly in the layout. This is the cleanest option because the styling stays in the view layer.

xml
1<TextView
2    android:id="@+id/titleText"
3    android:layout_width="wrap_content"
4    android:layout_height="wrap_content"
5    android:text="Important message"
6    android:textStyle="bold|italic" />

Valid values include normal, bold, italic, and the combined form bold|italic. This affects the entire text content of that TextView.

If you already use a custom font family, check whether that family actually includes bold and italic variants. Otherwise Android may simulate the style rather than using a true font weight.

Changing Style Programmatically

When styling depends on app state, update it in code. In modern Android projects, Kotlin is the usual choice:

kotlin
1val textView = findViewById<TextView>(R.id.titleText)
2
3textView.setTypeface(null, Typeface.BOLD)
4textView.text = "Bold title"

For italic:

kotlin
textView.setTypeface(null, Typeface.ITALIC)

For bold and italic together:

kotlin
textView.setTypeface(null, Typeface.BOLD_ITALIC)

This approach is useful when a validation error, selection state, or screen mode changes the appearance at runtime.

Styling Only Part of the Text

If only one phrase should be bold or italic, use a SpannableString or SpannableStringBuilder. That lets you apply styles to character ranges inside a single TextView.

kotlin
1import android.graphics.Typeface
2import android.text.SpannableString
3import android.text.Spanned
4import android.text.style.StyleSpan
5
6val message = SpannableString("Bold intro and italic ending")
7message.setSpan(
8    StyleSpan(Typeface.BOLD),
9    0,
10    4,
11    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
12)
13message.setSpan(
14    StyleSpan(Typeface.ITALIC),
15    15,
16    21,
17    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
18)
19
20textView.text = message

This is the right tool when one TextView contains labels, emphasis, or inline formatting.

Choosing Between XML and Code

Use XML when the style is fixed and tied to layout structure. It keeps your code simpler and is easier for designers and Android developers to scan.

Use programmatic styling when the appearance changes based on runtime logic. If only selected words need emphasis, spans are the most precise option.

You can also combine approaches. For example, a TextView may have a default XML style and then apply a span only to a warning keyword at runtime.

A Reusable Helper Example

If you apply the same styles repeatedly, extract a small helper:

kotlin
1fun TextView.applyHeadingStyle(isEmphasized: Boolean) {
2    setTypeface(
3        null,
4        if (isEmphasized) Typeface.BOLD_ITALIC else Typeface.NORMAL
5    )
6}

Usage:

kotlin
titleText.applyHeadingStyle(isEmphasized = true)

That keeps styling decisions centralized instead of scattering setTypeface calls across multiple screens.

Common Pitfalls

One common problem is using android:textStyle and expecting only part of the text to change. That attribute affects the whole TextView. For mixed formatting, use spans.

Another issue is losing a custom typeface by calling setTypeface(null, ...). If your app loads a specific font, preserve that Typeface instance and apply style carefully instead of resetting to null.

Developers also sometimes assume bold or italic will look identical on every device. Font rendering varies, and not every font family ships with all style variants. Test the result on actual devices or emulators.

Finally, span indexes are easy to get wrong. If the start or end positions do not match the real string length, the wrong substring gets styled or the code throws an exception. Compute indexes from the text itself when possible instead of hard-coding them.

Summary

  • Use android:textStyle in XML for simple whole-text styling.
  • Use setTypeface when the style changes at runtime.
  • Use SpannableString when only part of the text should be bold or italic.
  • Be careful when combining styles with custom fonts, because some font families do not provide every variant.
  • Treat spans as a precise tool for substrings, and validate index ranges carefully.

Course illustration
Course illustration

All Rights Reserved.