Set style for TextView programmatically
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
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:
Define the text appearance in res/values/styles.xml:
setTextAppearance() only applies text attributes — it ignores layout properties like background, padding, and gravity.
Setting Individual Properties
Kotlin Equivalents
Applying a Full Style at Creation Time
Use ContextThemeWrapper to apply a complete style when creating the view:
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:
Applying Spans for Partial Styling
For applying different styles to parts of the text:
Material Design Text Styles
With Material Components for Android:
Dynamic Style Switching
Common Pitfalls
setTextAppearanceignores non-text attributes: It only appliestextSize,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).setTypefaceresets the style: CallingtextView.setTypeface(newFont)clears bold/italic. To preserve the style, usetextView.setTypeface(newFont, Typeface.BOLD)or chain calls: set typeface first, then style.- No
setStyle()method on View: Android has noview.setStyle(R.style.MyStyle)method. Once a view is created, you cannot apply a full XML style. UseContextThemeWrapperat 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
ContextThemeWrapperto apply a full style at view creation time - Use
TextViewCompatfrom AndroidX for backward-compatible features - Use
SpannableStringfor mixed styles within a singleTextView - There is no way to apply a complete XML style to an already-created view
Related reading
- Set TextView text from html-formatted string resource in XML
- Set the layout weight of a TextView programmatically
- Set the maximum character length of a UITextField
- Set the maximum character length of a UITextField in Swift
- Set the maximum character length of a UITextField in Swift
- Set transparent background of an imageview on Android
- Set UIButton title UILabel font size programmatically
- Set UILabel line spacing
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.