Android - set TextView TextStyle programmatically?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want to change a TextView style in code, the most common tools are setTypeface, setTextAppearance, setTextColor, and setTextSize. Which one you should use depends on whether you are changing just bold or italic, applying a full text style resource, or updating one visual property dynamically.
For most apps, XML remains the best home for static styling. Programmatic styling is most useful when the text appearance depends on runtime state.
The Quick Bold and Italic Cases
If you only want to make existing text bold or italic, preserve the current typeface family and change the style:
Or:
This is usually better than setTypeface(null, Typeface.BOLD), because the null form can discard the currently configured font family.
Apply a Full Text Appearance
If you already have a style resource, applying that is cleaner than setting individual properties one by one:
This is a strong option when you want to keep styling centralized in resources but still decide at runtime which style to apply.
For example, you might switch between a normal and emphasized style depending on validation state or feature flags.
Change Individual Properties
For small dynamic adjustments, direct setters are fine:
Or from resources:
These work well for one-off state changes, such as marking invalid input or increasing emphasis after a user action.
Partial Styling With Spannable
If only part of the text should be bold or italic, do not restyle the whole TextView. Use a SpannableString instead:
This is the right tool when only a word or phrase needs emphasis.
Custom Fonts
If you need a specific font family, create a Typeface and apply it:
If you also want bold or italic styling, prefer a font file that already represents that weight or style when possible. Programmatic fake bold and fake italic are not always visually ideal.
Prefer Resources for Stable Design
Programmatic text styling is useful, but it is easy to overdo. If the same font, color, and size appear in many places, put them in XML styles or theme attributes first.
Use code when the style depends on runtime conditions such as:
- selected versus unselected state
- success versus error state
- premium versus standard UI mode
That keeps the styling logic understandable.
Common Pitfalls
The biggest pitfall is calling setTypeface(null, Typeface.BOLD) and accidentally losing the intended font family. Preserving the existing typeface is often safer.
Another common issue is setting many visual properties one by one in code when a style resource would be clearer and easier to maintain.
People also use whole-view styling when only part of the text needs emphasis. In those cases, SpannableString is the better tool.
Finally, if styling appears to do nothing, check whether a later call or state update is overriding the same property afterward.
Summary
- Use
setTypeface(textView.getTypeface(), Typeface.BOLD)for simple bold or italic changes. - Use
setTextAppearance(...)when you want to apply a full text style resource programmatically. - Use direct setters such as
setTextColororsetTextSizefor small dynamic changes. - Use
SpannableStringwhen only part of the text should be styled. - Keep stable design rules in XML and reserve code-based styling for runtime decisions.

