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"><b>bold text here</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?
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"><b>bold text here</b></string>
solution with getString()
works because you've encoded the less than sign.
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