Android
TextView
TextStyle
Programming
Android Development

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:

java
TextView title = findViewById(R.id.title);

title.setTypeface(title.getTypeface(), Typeface.BOLD);

Or:

java
title.setTypeface(title.getTypeface(), Typeface.ITALIC);

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:

java
TextView title = findViewById(R.id.title);
title.setTextAppearance(R.style.AppTitleText);

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:

java
title.setTextColor(Color.RED);
title.setTextSize(18f);

Or from resources:

java
title.setTextColor(ContextCompat.getColor(this, R.color.error_red));

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:

java
1SpannableString text = new SpannableString("Warning: action required");
2text.setSpan(new StyleSpan(Typeface.BOLD), 0, 8, 0);
3
4TextView label = findViewById(R.id.label);
5label.setText(text);

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:

java
Typeface typeface = ResourcesCompat.getFont(this, R.font.roboto_slab_bold);
title.setTypeface(typeface);

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 setTextColor or setTextSize for small dynamic changes.
  • Use SpannableString when only part of the text should be styled.
  • Keep stable design rules in XML and reserve code-based styling for runtime decisions.

Course illustration
Course illustration

All Rights Reserved.