Introduction
Android does not support applying a full XML style to a TextView after creation. The setTextAppearance() method applies text-related style attributes (size, color, typeface), while individual properties like background, padding, and gravity must be set through their own setter methods. For applying a complete style at creation time, use the ContextThemeWrapper constructor approach or MaterialTextView with a style overlay.
Setting Text Appearance
setTextAppearance() applies text-specific attributes from a style resource:
1// API 23+ (recommended)
2textView.setTextAppearance(R.style.MyTextAppearance);
3
4// API 1+ (deprecated but still works)
5textView.setTextAppearance(context, R.style.MyTextAppearance);
Define the text appearance in res/values/styles.xml:
1<style name="MyTextAppearance" parent="TextAppearance.AppCompat.Body1">
2 <item name="android:textSize">18sp</item>
3 <item name="android:textColor">#333333</item>
4 <item name="android:textStyle">bold</item>
5 <item name="android:fontFamily">sans-serif-medium</item>
6</style>
setTextAppearance() only applies text attributes — it ignores layout properties like background, padding, and gravity.
Setting Individual Properties
1TextView textView = new TextView(context);
2
3// Text size
4textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
5
6// Text color
7textView.setTextColor(Color.parseColor("#333333"));
8textView.setTextColor(ContextCompat.getColor(context, R.color.primary));
9
10// Bold, italic, bold-italic
11textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
12textView.setTypeface(textView.getTypeface(), Typeface.ITALIC);
13textView.setTypeface(textView.getTypeface(), Typeface.BOLD_ITALIC);
14
15// Custom font
16Typeface customFont = ResourcesCompat.getFont(context, R.font.roboto_medium);
17textView.setTypeface(customFont);
18
19// Background
20textView.setBackgroundColor(Color.WHITE);
21textView.setBackgroundResource(R.drawable.rounded_background);
22
23// Padding (in pixels)
24textView.setPadding(16, 8, 16, 8);
25
26// Gravity
27textView.setGravity(Gravity.CENTER);
28
29// Line spacing
30textView.setLineSpacing(4f, 1.2f); // add=4dp, mult=1.2
31
32// Max lines and ellipsis
33textView.setMaxLines(2);
34textView.setEllipsize(TextUtils.TruncateAt.END);
Kotlin Equivalents
1val textView = TextView(context).apply {
2 setTextAppearance(R.style.MyTextAppearance)
3 textSize = 18f // interpreted as SP
4 setTextColor(Color.parseColor("#333333"))
5 typeface = Typeface.DEFAULT_BOLD
6 gravity = Gravity.CENTER
7 setPadding(16, 8, 16, 8)
8 maxLines = 2
9 ellipsize = TextUtils.TruncateAt.END
10}
Applying a Full Style at Creation Time
Use ContextThemeWrapper to apply a complete style when creating the view:
1// Define style in styles.xml
2// <style name="CustomTextView">
3// <item name="android:textSize">20sp</item>
4// <item name="android:textColor">#FF5722</item>
5// <item name="android:background">@drawable/bg_rounded</item>
6// <item name="android:padding">12dp</item>
7// </style>
8
9ContextThemeWrapper wrappedContext = new ContextThemeWrapper(context, R.style.CustomTextView);
10TextView textView = new TextView(wrappedContext, null, 0);
The third parameter (0) tells the constructor not to apply a default style, using only the wrapped theme's style. This is the only way to apply non-text attributes (background, padding) from a style resource programmatically.
Using TextViewCompat
TextViewCompat from AndroidX provides backward-compatible methods:
1import androidx.core.widget.TextViewCompat;
2
3// Set text appearance (backward compatible)
4TextViewCompat.setTextAppearance(textView, R.style.MyTextAppearance);
5
6// Auto-size text (API 14+)
7TextViewCompat.setAutoSizeTextTypeWithDefaults(
8 textView,
9 TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM
10);
11
12// Auto-size with bounds
13TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
14 textView,
15 12, // min text size in SP
16 24, // max text size in SP
17 1, // step granularity in SP
18 TypedValue.COMPLEX_UNIT_SP
19);
Applying Spans for Partial Styling
For applying different styles to parts of the text:
1SpannableString spannable = new SpannableString("Hello World");
2
3// Bold "Hello"
4spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
5
6// Red "World"
7spannable.setSpan(new ForegroundColorSpan(Color.RED), 6, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
8
9// Larger "Hello"
10spannable.setSpan(new RelativeSizeSpan(1.5f), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
11
12textView.setText(spannable);
Material Design Text Styles
With Material Components for Android:
1// Use Material text appearances
2textView.setTextAppearance(com.google.android.material.R.style.TextAppearance_MaterialComponents_Headline6);
3textView.setTextAppearance(com.google.android.material.R.style.TextAppearance_MaterialComponents_Body1);
4textView.setTextAppearance(com.google.android.material.R.style.TextAppearance_MaterialComponents_Caption);
5
6// Material 3 (Material You)
7textView.setTextAppearance(com.google.android.material.R.style.TextAppearance_Material3_BodyLarge);
Dynamic Style Switching
1public void applyTheme(TextView textView, boolean isDarkMode) {
2 if (isDarkMode) {
3 textView.setTextColor(Color.WHITE);
4 textView.setBackgroundColor(Color.parseColor("#121212"));
5 } else {
6 textView.setTextColor(Color.parseColor("#333333"));
7 textView.setBackgroundColor(Color.WHITE);
8 }
9}
10
11// Or use a style resource
12textView.setTextAppearance(isDarkMode
13 ? R.style.DarkTextAppearance
14 : R.style.LightTextAppearance);
Common Pitfalls
setTextAppearance ignores non-text attributes: It only applies textSize, textColor, textStyle, fontFamily, and similar text properties. Background, padding, margins, and layout parameters must be set separately through their own methods.
setTextSize() default unit is SP, not DP: textView.setTextSize(18) uses SP (scaled pixels). To use DP or PX, specify the unit: textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18).
setTypeface resets the style: Calling textView.setTypeface(newFont) clears bold/italic. To preserve the style, use textView.setTypeface(newFont, Typeface.BOLD) or chain calls: set typeface first, then style.
No setStyle() method on View: Android has no view.setStyle(R.style.MyStyle) method. Once a view is created, you cannot apply a full XML style. Use ContextThemeWrapper at creation time or set individual properties afterward.
Padding in pixels: setPadding() takes pixel values, not dp. Convert dp to pixels first: int px = (int) (dpValue * context.getResources().getDisplayMetrics().density).
Summary
Use setTextAppearance(R.style.X) for text-specific attributes (size, color, typeface)
Set individual properties (setTextSize, setTextColor, setTypeface) for fine-grained control
Use ContextThemeWrapper to apply a full style at view creation time
Use TextViewCompat from AndroidX for backward-compatible features
Use SpannableString for mixed styles within a single TextView
There is no way to apply a complete XML style to an already-created view