I have a string defined in string.xml
like
<string name="eventtitle">Title: %1$s </string>
which is formatted using string.format
. How to define the string to get only the Titel: as Bold.
Thanks in advance for the help
android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”. If you want to use bold and italic. Use pipeline symbol “|” .
Displaying the string in some email clients will show as bold if you surround it with asterisks (*).
Just use getText(R. string.
text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.
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, <b>this is bold</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:
Now, it is reasonably supported by Android.
you can define the string in xml as <string name="text_to_show">Hey <b>This is in bold</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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With