Set TextView text from html-formatted string resource in XML
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Android string resources can include lightweight HTML-style markup for emphasis, links, and simple formatting. The correct flow is to keep markup in strings.xml, parse it to a spanned value in code, and assign that to TextView. This keeps localization manageable while still delivering formatted UI text.
Define HTML in strings.xml
Store minimal, supported tags in string resources. Use CDATA for readability.
Keep markup simple. Deeply nested tags are hard for translators and easier to break.
Parse HTML with HtmlCompat
Do not assign raw HTML directly. Parse first:
This converts supported HTML tags into Android spans.
Make Links Clickable
If resource includes anchor tags, enable movement method.
Without movement method, links may appear styled but not respond to taps.
Interpolate Dynamic Values Safely
If you insert runtime values into HTML templates, escape user data first so markup is not corrupted.
This prevents accidental broken tags from characters in user-provided names.
Theme and Accessibility Considerations
Inline hardcoded colors in HTML can fail in dark mode. Prefer theme-driven styling where possible. If legal or marketing copy needs specific emphasis color, verify contrast in both light and dark themes.
Also test dynamic type and larger font sizes. Long spanned text can wrap differently than plain text and expose layout issues in constrained containers.
Performance in RecyclerView and Repeated Rendering
HTML parsing is relatively cheap for short strings but can still be noticeable when repeated in fast scrolling lists. For repeated content:
- Parse once and cache spanned text in view model or adapter data.
- Avoid reparsing in every bind call.
Precomputing spans improves rendering consistency in list-heavy screens.
When to Prefer Native Spans Instead of HTML
For complex formatting rules, native span builders can be safer than HTML strings because they avoid translator-facing tag syntax.
Use resource HTML for simple emphasis and links. Use native spans when formatting depends on runtime state, feature flags, or theme logic that is difficult to express in static markup.
Test Strategy
Keep small UI checks for critical formatted strings:
- Verify text content is displayed.
- Verify expected span types exist.
- Verify link clickability.
- Verify localization variants do not break tags.
Basic instrumentation assertion example:
Pair this with screenshot checks for high-risk legal or onboarding text.
Common Pitfalls
- Assigning raw HTML string directly to
TextViewwithout parsing. - Expecting full web HTML and CSS behavior in Android text rendering.
- Forgetting movement method for link interactivity.
- Inserting unescaped dynamic values into HTML templates.
- Using hardcoded inline colors that break dark-mode readability.
Summary
- Store simple HTML markup in string resources for localized formatted text.
- Parse with
HtmlCompat.fromHtmlbefore assigning toTextView. - Enable link movement method when strings contain anchors.
- Escape runtime values before formatting HTML templates.
- Cache parsed spans in performance-sensitive list rendering paths.
Related reading
- Set useState hook in a async loop
- setTimeout in React Native
- sh 1 react-scripts not found In Docker
- sh husky command not found
- Set the layout weight of a TextView programmatically
- Set the maximum character length of a UITextField
- Should CSS always precede JavaScript?
- Should I add async/await to a single-line function or not?
.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.