The Most Appropriate way to show html formatted text from string resource xml file --

Hello. From the title itself I think my situation is pretty clear;

In my strings xml file, one of my resources is like this:

<string name="txtTrivia"> <p>... Well, there we were, the three of us, Victor, Leonardo and myself on our way back home after a stroll around the streets, on one cloudy, expectant afternoon. ..</p> <p> Victor, the huge big burly guy and the ever-hungry soul that he always is, felt his stomach growling again. So we stopped by and got this pizza with our combined finances. We took the short cut through the park, in a hurry to reach home and finish the pizza, before it cooled off ... .. </p> <p>Rain! Vindictive, thunderous, blinding rain. As if the heavens have opened up, you know! We ran to take shelter under a tree. It was an old, huge tree. And famous too in the neighbourhood; everybody called it the ‘Wishing Tree’. ... . </p> <p>‘What?’ I asked. ‘yeah, What?’ exclaimed Leo too! A good 2 minutes of silence prevailed all around. ... . ... </p> </string>

Now in my .axml file I have a textview where I want to fetch this text and show it, maintaining the html formatting intact. The id of the control was 'sometext'. Now, in mainActivity, I did this code:

TextView foo = (TextView)FindViewById(Resource.Id.sometext); var Triviatxt = String.Format(Resources.GetString(Resource.String.txtTrivia)); foo.TextFormatted = Html.FromHtml(Triviatxt);

But application is not working as expected. My app gets deployed and immediately after, gives the hung/error alert. See the screenshots here:

Can you tell me how to correct the code for this? Also I want to know what other html tags I can use without problem in my text, other than 'p' tags; because I would like to use a few more to mark my text content as per liking.

Any kind of guidance here will be much appreciated.


  • CraigDunnCraigDunn USXamarin Team Xamurai

    Moving this to the Android forum.

  • CheesebaronCheesebaron DKInsider, University mod

    As I wrote here not so long ago:

    These tags are allowed:

    <font> (with size and color attributes)

    You might need to prepend with <![CDATA[ and append with ]]> so the stuff in your <string> does not get interpreted as part of the rest XML.

  • PGChoudhuryPGChoudhury INMember

    Hii, @Cheesebaron.

    Thanks for replying back.
    Thing is, I had already tried the CDATA trick along with the same C# code that I pasted earlier. The errors are the same, the screenshots of which I earlier provided. Ok, cool, let me give it another clean, organised try and report back with exactly what I see.

    It is extremely good to know that I can use that many of the rest of the HTML tags too. I was particularly hoping for 'br' and 'bold'.
    Also, I just wanted to ask, the rest of those HTML tags, I presume I can use them as it is, just like my 'p' tag and not have to bother nothing about ugly lt; gt; stuff. Just wanted to be sure here.

    Will keep you all posted on what happens with this.


  • CheesebaronCheesebaron DKInsider, University mod

    I have some strings which look like this in my app:

    <string name="legal_mvx_description">
        <![CDATA[Copyright ⓒ 2014 Stuart Lodge\nLicense: Ms-Pl\nWebsite: ]]> <a href=""></a>

    I guess you just need to make sure to escape XML characters and you should be good.

Sign In or Register to comment.