Bold words in a string of strings.xml in Android
To bolden words directly in your strings.xml
, use HTML <b>
tags:
Then, bring out the magic of Html.fromHtml()
in Java to apply the bold style:
Dealing with old school Android versions below API 24? No worries, mate:
Congrats, your text is now bolder across all Android versions. 🌍
Making words pretty: Spanning the Android text-scape
Advanced text tricks: Spannable strings
For dynamic text, use SpannableString
and SpannableStringBuilder
:
Mixing static and dynamic parts: The beauty of placeholders
Placeholders (%1$s
) are priceless when dealing with dynamic values:
Just replace the placeholders in your Java/Kotlin code:
Escaping HTML tags: Survival of the prettiest
You got a string that looks like an HTML tag? Make sure to escape them:
Kotlin: Who says Java gets to have all the fun?
Kotlin and Android, sitting in a tree, E.X.T.E.N.D.I.N.G
Looking for all the fun with Kotlin? Create extension functions that make conversion easier:
Playing nice and safe: TextUtils.htmlEncode
For special characters, TextUtils.htmlEncode
is your trusted sidekick:
Compatibility: The secret to every long-lasting relationship
When SDK plays hard to get
Just like you check the milk's expiry date, make sure to keep an eye on Build.VERSION.SDK_INT
:
getText() vs getString(): There can only be one
When it comes to styled resources, always place your bets on getText()
:
HTML within CDATA: Because everyone deserves a safe space
For a cleaner approach to HTML formatting, wrap your code inside a CDATA section:
Was this article helpful?