Jetpack Compose: Styled Text from String Resources
In this article
You can display styled text from string resources in Jetpack Compose by putting HTML-style tags such as <b>, <i> and <u> inside the string, then rendering it with AnnotatedString.fromHtml() (available from Compose 1.7) or by parsing the resource with Html.fromHtml() and converting it to an AnnotatedString for older versions. The shortest working path is: define the string in res/values/strings.xml with inline tags, read it with stringResource(R.string.your_string), and pass the resulting AnnotatedString to a Text composable. This keeps translations in the resource system while still letting you bold a word, italicise a phrase or underline a link without hardcoding styling in Kotlin.
What You'll Need
- Android Studio with a project that already uses Jetpack Compose (the Compose BOM is set up in your module's
build.gradle). - A
strings.xmlfile underres/values/(and translated variants underres/values-xx/if you localise). - Compose UI 1.7 or newer if you want to use
AnnotatedString.fromHtml(). Check the Compose BOM version in your build file; if it is older, theHtml.fromHtml()route below still works. - Basic familiarity with
stringResource()and theTextcomposable.
Step-by-Step
- Add the styled string to
strings.xml. Use HTML-style tags around only the words you want styled. For example:
Keep the tags inside the string value, not in a separate style file. Escape an ampersand as<string name="welcome_message">Welcome to <b>TechBuzzPro</b>. Read the <i>terms</i> before you continue.</string>&and a less-than sign as<if they are part of the visible text rather than a tag. - Read the string in your composable. Call
stringResource(R.string.welcome_message)inside a@Composablefunction. This returns a plainStringthat still contains the tags, which is what you want at this stage. - Convert the tagged string to an
AnnotatedString. On Compose 1.7 and newer, use:
On older Compose versions, parse with the Android HTML helper and convert:val styled = AnnotatedString.fromHtml(stringResource(R.string.welcome_message))
Theval raw = stringResource(R.string.welcome_message) val styled = Html.fromHtml(raw, Html.FROM_HTML_MODE_LEGACY).toAnnotatedString()toAnnotatedString()extension lives inandroidx.compose.ui.text. Import it explicitly if your IDE does not add it automatically. - Pass the result to
Text. TheTextcomposable accepts anAnnotatedStringdirectly:
The outerText( text = styled, style = MaterialTheme.typography.bodyLarge )stylesets the base font, size and colour; the tags inside the string only override bold, italic and underline on the spans they wrap. - Preview and check alignment with translations. Open the Compose preview or run the app on a device and confirm the bold and italic words render as expected. Then open each translated
strings.xmland make sure the same tags wrap the equivalent words. If a translator drops a tag, that language simply loses the emphasis; it will not crash, but the layout may look inconsistent. - Handle links if you need them.
AnnotatedString.fromHtml()supports<a href="...">tags and produces a link annotation. To make the link clickable, wrap theTextinClickableText(older API) or useLinkAnnotationwithTextand apointerInputtap handler, reading the annotation at the tapped offset. For most apps, a plain coloured, underlined span is enough; only add the click handler if the link must actually open something. - Keep the tags minimal. Stick to
<b>,<i>,<u>and<a>unless you have checked that your Compose version maps the tag you want. Tags such as<font color>and<span style>are not guaranteed to survive the conversion, so apply colour through theTextstyle or aSpanStyleinstead.
Common Mistakes
- Passing the raw string to
Text. If you skip the conversion step, the user sees the literal<b>tags on screen. Always convert toAnnotatedStringfirst. - Using the wrong HTML mode. With
Html.fromHtml(), passHtml.FROM_HTML_MODE_LEGACY; the deprecated single-argument overload behaves differently across API levels and can drop line breaks. - Forgetting to escape literal angle brackets. A string that contains
<as text rather than as a tag must be escaped, or the parser treats it as the start of an unknown tag and the visible output changes. - Assuming all HTML works. Compose maps a small set of tags. Complex markup such as tables, nested lists or CSS classes is not rendered; it is flattened to plain text.
- Hardcoding styled text in Kotlin. Building the
AnnotatedStringwithbuildAnnotatedStringand literal English words bypasses translation. Keep the words in resources and only the tags in the string. - Ignoring RTL languages. Tags do not affect direction, but if you mix left-to-right and right-to-left text inside one string, test the layout in an RTL locale to make sure the bold span does not break the visual order.
Tips
- Use a lint-friendly naming convention such as
message_xxx_htmlfor strings that contain tags, so translators and reviewers know markup is expected. - If only one word needs emphasis, consider splitting the string into three resources and composing them with
buildAnnotatedStringandappend. This avoids HTML parsing entirely and is easier to translate. - Set the base look with
MaterialTheme.typographyand acoloronText; let the tags handle only emphasis. Mixing both sources of styling makes the result hard to predict. - For accessibility, styled spans are read by TalkBack as normal text, so the emphasis is visual only. If the bold word carries meaning, add it to a content description or rephrase.
- Keep a small unit test that asserts the converted
AnnotatedStringcontains the expected number of bold spans; it catches a translator accidentally removing a tag.
Troubleshooting
If the tags appear literally on screen, you are passing the raw String to Text; add the AnnotatedString.fromHtml() or Html.fromHtml() conversion. If the text renders but the bold is missing, check that the tag is inside the string value and that the Compose version supports the tag you used. If the app crashes with a NoSuchMethodError on fromHtml, your Compose BOM is older than 1.7; either upgrade the BOM or switch to the Html.fromHtml() route. If line breaks disappear, use FROM_HTML_MODE_LEGACY and put \n or <br> where you need them. If a link is not clickable, remember that Text does not handle clicks by default; add a tap handler that reads the link annotation at the tapped position. If the styled text looks wrong only in one language, compare that translation's tags against the default strings.xml; a missing closing tag is the usual cause.
Frequently Asked Questions
Does AnnotatedString.fromHtml() work on all Compose versions?
No. It was added in Compose UI 1.7. On older versions use Html.fromHtml() with FROM_HTML_MODE_LEGACY and convert the result with toAnnotatedString().
Can I use <font color> or CSS in the string?
Not reliably. Compose maps a limited set of tags. Apply colour and size through the Text style or a SpanStyle instead of inline HTML attributes.
Will the tags break my translations?
No, as long as translators keep the tags around the equivalent words. A dropped tag only removes emphasis in that language; it does not crash the app.
How do I make a link inside the string clickable?
Parse the string with AnnotatedString.fromHtml(), then add a tap handler that reads the LinkAnnotation at the tapped offset and opens the URL with an Intent. A plain Text composable does not handle clicks on its own.
Should I use HTML tags or buildAnnotatedString?
Use HTML tags when the whole sentence lives in one string resource and only a few words need emphasis. Use buildAnnotatedString when you need to combine several resources or apply styles that HTML does not support.
Do styled spans affect accessibility?
TalkBack reads the text normally; bold and italic are visual only. If the emphasis changes the meaning, state it in the surrounding text or a content description.