Saturday, 23 January 2016

HTML formatted string resource in XML

I am going to write HTML formatted string resource in XML file and will show you how to bind in TextView.

You can add raw html like that
<![CDATA[ ...raw html... ]]>

I have some fixed text in string XML file.

string.xml
<resources>
    <string name="app_name">HtmlTextResource</string>
  <string name="html_data_text"><![CDATA[
<p>This is a html-formatted string with <b>bold</b> and <i>italic</i> text</p>
<p>This is another paragraph of the same string.</p>
 <a href=\'https://github.com/'>Github</a>&nbsp;&nbsp;
       <a href=\'https://www.google.co.in/?gfe_rd=cr&ei=uGOjVrC1C4LC8gfDtqngDA&gws_rd=ssl\'>Google</a>&nbsp;&nbsp;
       <a href=\'http://mail.google.com/mail\'>Gmail</a>

]]></string>
</resources>


Binding string data in TextView

TextView textView= (TextView) findViewById(R.id.tvHtmlText);
        Spanned aboutBody = Html.fromHtml(getResources().getString(R.string.html_data_text));
        textView.setText(aboutBody);
        textView.setMovementMethod(LinkMovementMethod.getInstance());