Set style for TextView programmatically
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.