103

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?

Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
Alberto Alberto
  • 1,031
  • 2
  • 8
  • 6
  • Check out: [Set TextView text from html-formatted string resource in XML](http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml) – Sam Nov 16 '12 at 22:08

5 Answers5

238

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());
Sagar Kacha
  • 8,338
  • 4
  • 18
  • 27
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
  • 9
    You can use HTML _without_ CDATA if you just use `getText()` instead of `getString()`: http://stackoverflow.com/a/18199543/89818 – caw Aug 13 '13 at 02:07
  • 19
    Yes, but with `CDATA` the actual HTML you include is much easier - no need to translate all the <, >, etc. Just copy the real HTML, and paste into your strings.xml – Richard Le Mesurier Nov 09 '13 at 11:39
  • 1
    Thanks, works well. I just would like to know how to center text vertically in the textview. – Hermandroid Feb 03 '16 at 17:13
  • 7
    select text for which you want CDATA.. and press ctrl+alt+T -> select 'Surrounf with CDATA section' – Prashant Jajal Oct 24 '16 at 05:22
  • Sorry but it doesn't work. The only valid solution I've found working wih string, u, i and all supported tag for Html.from is the one from wsanville, so use &lt and &gt for opening and closing the HTML tag. – Peter Sep 05 '17 at 12:46
30

Here's most of the examples. I don't think the pre tag is supported.

enter image description here

This is the strings.xml file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Formatting</string>
    <string name="link">&lt;b&gt;Hello World&lt;/b&gt; This is a test of the URL &lt;a href="http://www.example.com/"&gt;Example&lt;/a&gt;</string>
    <string name="bold">&lt;b&gt;This text is bold&lt;/b&gt;</string>
    <string name="emphasis">&lt;em&gt;This text is emphasized&lt;/em&gt;</string>
    <string name="sup">This is &lt;sub&gt;subscript&lt;/sub&gt; and &lt;sup&gt;superscript&lt;/sup&gt;</string>
</resources>

Here's the layout. Note for the link to actually be clickable, there's a bit of extra work needed:

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/test1"
            android:linksClickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
        <TextView
            android:id="@+id/test4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="12dp"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>
    </LinearLayout>
</ScrollView>

Finally, the code:

TextView test1 = (TextView)findViewById(R.id.test1);
Spanned spanned = Html.fromHtml(getString(R.string.link));
test1.setMovementMethod(LinkMovementMethod.getInstance());
test1.setText(spanned);

TextView test2 = (TextView)findViewById(R.id.test2);
test2.setText(Html.fromHtml(getString(R.string.bold)));

TextView test3 = (TextView)findViewById(R.id.test3);
test3.setText(Html.fromHtml(getString(R.string.emphasis)));

TextView test4 = (TextView)findViewById(R.id.test4);
test4.setText(Html.fromHtml(getString(R.string.sup)));
wsanville
  • 37,158
  • 8
  • 76
  • 101
6

String.xml can contains HTML entities, like so:

<resources>
    <string name="hello_world">&lt;span&gt;</string>
</resources>

In your code: getResources().getString(R.string.hello_world); will evaluate to "<span>". You can use this HTML formatted text like this:

TextView helloWorld = (TextView)findViewById(R.id.hello_world);
helloWorld.setText(Html.fromHtml(getString(R.string.hello_world)));
Thomas
  • 1,508
  • 2
  • 22
  • 35
3

All the styling supported by the XML resources system is explained in the Android documentation.

String Resources: Formatting and Styling

Anything included there can be used and set directly on TextView. If you need to use further HTML markup, you will need to place raw HTML (with escaped characters for &lt;, &gt; and such) into the resource and load the entire thing in a WebView.

devunwired
  • 62,780
  • 12
  • 127
  • 139
1

This Worked for me :

<?xml version="1.0" encoding="utf-8"?>

<string name="app_name">Sangamner College</string>
<string name="about_desc"><![CDATA[In order to make higher education available in the rural environment such as of Sangamner, Shikshan Prasarak Sanstha was established in 1960. Sangamner College was established by Shikshan Prasarak Sanstha, Sangamner on 23rd January 1961 on the auspicious occasion of Birth Anniversary of Netaji Subhashchandra Bose.The Arts and Commerce courses were commenced in June 1961 and in June 1965 Science courses were introduced. When Sangamner College was founded forty years ago, in 1961, there was no college available to the rural youth of this region. <br><br></>The college was founded with the aim of upliftment of the disadvantageous rural youth in all respects. On one hand, we are aware of the social circumstances prevailing in the rural area where we are working. So, we offer the elective option to students, which are favourable to the local atmosphere. On the other hand, we want to academically empower the aspiring youth by offering vocational course in Computer Applications to students of Arts &amp; Commerce. B.B.A., B.C.A. and M.C.A. courses were started with the same purpose. “Think globally, act locally” is our guiding Principle.]]></string>

Suyog Gunjal
  • 287
  • 4
  • 7