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.
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:
For italic:
For bold and italic together:
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.
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:
Usage:
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:textStylein XML for simple whole-text styling. - Use
setTypefacewhen the style changes at runtime. - Use
SpannableStringwhen 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.

