Bold words in a string of strings.xml in Android
Bold words in a string of strings.xml in Android
Normally use html tags in the string resource like below,
<resource> <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string> </resources>
Also use Html.fromHtml else use spannable.
Try the given codes that uses html tag inside string resources,
<resources> <string name="string_resource_name"><![CDATA[<b> Your text </b>]]> </string> </resources>
Get bold text from string resources like,
private Spanned getSpannedText(String text) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT); } else { return Html.fromHtml(text); } } String s = format(context.getResources().getString(R.string.string_resource_name)); textView.setText(getSpannedText(s));
Use the HTML in string resources:
<resource> <string name="my_string">A string with <i>actual</i> <b>formatting</b>!</string> </resources>
Later use the getText(R.string.my_string) method alternative to getString(R.string.my_string), then user may get back a CharSequence alternative than a string which consists the formatting embedded.
<resource>
<string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
</resources>
private Spanned getSpannedText(String text) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT);
} else {
return Html.fromHtml(text);
}
}
String s = format(context.getResources().getString(R.string.string_resource_name));
textView.setText(getSpannedText(s));
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="total_review"><b>Total Review: </b> </string>
</resources>