Search code examples
androidhtmltextview

HTML formatting in TextView


I have the following in my string resource xml file.

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

<string name="hello">Hello World, ProDroid_Android!</string>
<string name="app_name">ProDroid_Android-CH3-Resources</string>

<plurals name ="eggs_in_A_basket">
    <item quantity ="one"> There is 1 egg</item>
    <item quantity = "other"> There are %d eggs</item>
    
</plurals>
<string name="simple_string">simple string</string>
<string name="quoted_string">"Quoted String 'xyz' string"</string>
<string name="double_quoted_string">\"Double Quoted String\""\";</string>
<string name="java_format_string">Hello %2$s Java format String. %1$s again.</string>

<string name="tagged_string">Hello <b><i> Slanted Android.</i></b>, You are bold</string>

</resources>

When I go to put the tagged_string into a TextView with the HTML formatting it does not display with its formatting. However, if I cut and paste the Hello Slanted Android., You are bold and manually put it into .setText it works, or if I set it statically in my layout XML file.

However using the Java code below it does not work (all you see is unformatted text):

//HTML
        TextView t6 = (TextView) findViewById(R.id.textView6);
                        
        t6.setText(Html.fromHtml(ProDroid_Android.this.getString(R.string.tagged_string)));

What can I try next?


Solution

  • If you format your text in XML it isn't necessary to use Html.formatHtml(...)

    Try this:

    t6.setText(getString(R.string.tagged_string);
    

    EDIT: Hmm... This way worked for me fine and I tried exactly your String. Maybe your Project needs a clean (Project > Clean ...)

    If this doesn't help: I found another question. Maybe the answer there can solve your problem