Android: html in strings.xml
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());
Here's most of the examples. I don't think the pre
tag is supported.
This is the strings.xml
file:
<?xml version="1.0" encoding="utf-8"?><resources> <string name="app_name">Formatting</string> <string name="link"><b>Hello World</b> This is a test of the URL <a href="http://www.example.com/">Example</a></string> <string name="bold"><b>This text is bold</b></string> <string name="emphasis"><em>This text is emphasized</em></string> <string name="sup">This is <sub>subscript</sub> and <sup>superscript</sup></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)));
String.xml can contains HTML entities, like so:
<resources> <string name="hello_world"><span></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)));