android: string format specify bold

Actually, many of the answers are obsolete. After researching by myself, what I've found out that the best answer is one by @Wahib. Here's the improved version:

Define the string resource as a:

<string name="styled_text">Hey, &lt;b>this is bold&lt;/b> text</string>

Use the resource like this:

String text = getResources().getString(R.string.styled_text);
CharSequence styledText = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY);
textview.setText(styledText);

Here's the result:

enter image description here


You can do it like,

textView.setText(Html.fromHtml("<b>Title</b>: Text"));

If you have text in dynamic way..

And to define formatings in Strings.xml you can do like,

<string name="text1">This text uses <b>bold</b> and <i>italics</i> 
by using inline tags such as <b> within the string file.</string>

see This Link


Now, it is reasonably supported by Android.

you can define the string in xml as <string name="text_to_show">Hey &lt;b>This is in bold&lt;/b>

Then in code, use this to convert to CharSequence and then use it for e.g in TextView

String text = getResources().getString(R.string.text-to_show);
CharSequence styledText = Html.fromHtml(text);
textview.setText(styledText);

Tags:

Android