Take the 2-minute tour ×

Would appreciate your help: I need to put HTML-formatted text (simple stuff, with only a few <b>/</b> and <i>/</i> markers here and there) into a TextView. Easy enough, but in my case, I need to combine three different lines of such text, each stored in a string resource. Something like this:

(TextView) newText.setText(getString(R.string.textstr1) + getString(R.string.textstr2) + getString(R.string.textstr3));

won't work because concatenating them as Strings drops the HTML formatting. I tried solving it by using getText() instead of getString() and CharSequence.append(), but that also drops the HTML. I tried various options using HTML.fromHTML(), but none of them worked either. Android Developer presents this crude approach where you have to use escaped HTML (< b > becomes & lt ; b > and so on), but that would require reformatting all of my string resources and strikes me as a kludge.

Does anyone know of a better way to do what I'm trying to do?

share|improve this question
Duplicates of stackoverflow.com/questions/8061796/… –  RC. Nov 10 '11 at 7:33
add comment

3 Answers

Method getString eliminates HTML tags, but there are two tricks available:

  1. Use & lt; and & gt; instead of < and >. That would prevent getString from cutting tags out and still alow Html.fromHtml() to parse them.


<string name="text">This is &lt;i&gt;italic&lt;/i&gt;</string>
  1. Use CDATA to wrap all string with HTML tags.


<string name="text"><![CDATA[This is <i>italic</i>]]></string>

In both cases all you have to do next is just setText.


If you want some more examples check: http://android4beginners.com/2013/07/lesson-1-5-how-to-color-part-of-the-text-and-use-alpha-channel-in-textview/ (font color HTML tags in strings.xml)

share|improve this answer
add comment

Try this, Hope it will help..

String newString=getString(R.string.textstr1) + getString(R.string.textstr2) + getString(R.string.textstr3);

share|improve this answer
I tried this before; unfortunately, it works even worse as it not only eliminates the HTML font formatting but also the '\n' line breaks. –  Lon Nov 10 '11 at 8:02
add comment
(TextView) newText.setTextHtml.fromHtml((getString(R.string.textstr1) + getString(R.string.textstr2) + getString(R.string.textstr3)));
share|improve this answer
setTextHtml is not a method of TextView, so this only gets me an error message. Did you mean newText.setText(Html.fromHtml(... ? That would make it the same as the previous suggestion. –  Lon Nov 10 '11 at 8:06
add comment

Your Answer


