Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: html in strings.xml

I would like display for example this html code:

<body>     <p><b>Hello World</b></p>     <p>This is a test of the URL <a href="http://www.example.com"> Example</a></p>     <p><b>This text is bold</b></p>     <p><em>This text is emphasized</em></p>     <p><code>This is computer output</code></p>     <p>This is<sub> subscript</sub> and <sup>superscript</sup></p> </body> 

I want to display it on a Dialog by declaring html in resources strings.xml. How can I do it?

like image 480
Alberto Alberto Avatar asked Nov 16 '12 21:11

Alberto Alberto


1 Answers

The best way to add html source code in strings.xml is to use <![CDATA[html source code]]>. Here is an example:

<string name="html"><![CDATA[<p>Text</p>]]></string>  

Then you can display this html in TextView using:

myTextView.setText(Html.fromHtml(getString(R.string.html))); 

If you have links in your html and you want them to be clickable, use this method:

myTextView.setMovementMethod(LinkMovementMethod.getInstance()); 
like image 145
4 revs, 2 users 96% Avatar answered Sep 30 '22 19:09

4 revs, 2 users 96%