Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML tags not working within strings.xml

I was trying to make bold text within my strings.xml using the HTML tag <b> and </b> as per this answer:

<string name="name"><b>bold text here</b></string>

And then trying to set the text as follows:

TextView textview = new TextView(getActivity());
textview.setText(getString(R.string.name));

However this is not working at all, the tags are ignored and the text is displayed regularly. I have been able to make the text bold using the following method:

<string name="name">&lt;b>bold text here&lt;/b></string>

And with this code in Java:

TextView textview = new TextView(getActivity());
textview.setText(Html.fromHtml(getString(R.string.name)));

Why will the simple HTML tags not work yet this other method works just fine?

like image 534
TronicZomB Avatar asked Mar 16 '14 23:03

TronicZomB


1 Answers

Use getText(int) instead of getString(int).

Why will the simple HTML tags not work yet this other method works just fine?

Because the getString(int) method will return a string, that is stripped of styled text information, like the docs say.

As per the Android developer guidelines the getText(int) method does not:

getText(int) will retain any rich text styling applied to the string.

The <string name="name">&lt;b>bold text here&lt;/b></string> solution with getString() works because you've encoded the less than sign.

like image 158
Ahmad Avatar answered Nov 15 '22 17:11

Ahmad