Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: string format specify bold

Tags:

android

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

like image 610
png Avatar asked Dec 12 '11 08:12

png


People also ask

How do I make string bold on Android?

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 “|” .

How do I make my text string bold?

Displaying the string in some email clients will show as bold if you surround it with asterisks (*).

How do I make a string bold in XML?

Just use getText(R. string.

How do I make text bold in textView android programmatically?

text. style. StyleSpan(Typeface. BOLD), start, end, Spannable.


2 Answers

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

like image 100
bmilovanovic Avatar answered Nov 15 '22 11:11

bmilovanovic


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);
like image 33
Wahib Ul Haq Avatar answered Nov 15 '22 11:11

Wahib Ul Haq