Explain Codes LogoExplain Codes Logo

Bold words in a string of strings.xml in Android

java
spannable-strings
html-formatting
text-utils
Alex KataevbyAlex Kataev·Jan 30, 2025
TLDR

To bolden words directly in your strings.xml, use HTML <b> tags:

<string name="bold_text">This is <b>bolded</b> text</string>

Then, bring out the magic of Html.fromHtml() in Java to apply the bold style:

// Deploying our secret weapon on the unsuspecting text here textView.setText(Html.fromHtml(getString(R.string.bold_text), Html.FROM_HTML_MODE_COMPACT));

Dealing with old school Android versions below API 24? No worries, mate:

// Boldness knows no age textView.setText(Html.fromHtml(getString(R.string.bold_text)));

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:

// SpannableStringBuilder: Because Your String Deserves More Than Just Regular Beauty Treatment SpannableStringBuilder spannableBuilder = new SpannableStringBuilder("Regular bold italic"); spannableBuilder.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 8, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannableBuilder.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC), 13, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(spannableBuilder);

Mixing static and dynamic parts: The beauty of placeholders

Placeholders (%1$s) are priceless when dealing with dynamic values:

<string name="welcome_message">Welcome to the <b>%1$s</b> world!</string>

Just replace the placeholders in your Java/Kotlin code:

// First step: replacing scary placeholders with the real deal String userName = "Bold"; String welcomeText = String.format(getString(R.string.welcome_message), userName); // Step 2: apply spannable magic dust Spanned formattedText = Html.fromHtml(welcomeText, Html.FROM_HTML_MODE_COMPACT); textView.setText(formattedText);

Escaping HTML tags: Survival of the prettiest

You got a string that looks like an HTML tag? Make sure to escape them:

// Nothing to see here, definitely not an HTML tag <string name="example">This tag is innocent: &lt;b&gt;NOT HTML&lt;/b&gt;</string>

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:

// All hail the mighty extension function! fun String.fromHtmlCompat(): Spanned = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Html.fromHtml(this, Html.FROM_HTML_MODE_COMPACT) } else { Html.fromHtml(this) } // And here it is, in action textView.text = getString(R.string.bold_text).fromHtmlCompat()

Playing nice and safe: TextUtils.htmlEncode

For special characters, TextUtils.htmlEncode is your trusted sidekick:

String safeString = TextUtils.htmlEncode(userInput); // The name’s String. Safe String.

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:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // No one's too good for my bold text textView.setText(Html.fromHtml(getString(R.string.bold_text), Html.FROM_HTML_MODE_COMPACT)); } else { // Old but gold textView.setText(Html.fromHtml(getString(R.string.bold_text))); }

getText() vs getString(): There can only be one

When it comes to styled resources, always place your bets on getText():

// The only time when TEXTing is better than getting STRINGS attached CharSequence styledText = getText(R.string.bold_text); textView.setText(styledText);

HTML within CDATA: Because everyone deserves a safe space

For a cleaner approach to HTML formatting, wrap your code inside a CDATA section:

// Building a safe haven for HTML. A layman would say 'A VIP Lounge' <string name="rich_text"><![CDATA[This is a <b>bold</b> move!]]></string>