How to format text in a TextView?

3

I want to add formatted text (can be in html) on my TextView screen but I'm not getting it.

I have the string in the values:

 <string name="lbl_explicacao">
          <![CDATA[
       <b> TESTE </b>
        TESTE
        TETES
        ]]>
    </string>

I have this Html.fromHtml(String, flags) , but I do not know how to use it

The text that I want to do is kind of big, with 283 words, but I would like to format the title and such ..

I tried to use the string

 <string name="lbl_explicacao"> <![CDATA[
 <b>What is Lorem Ipsum?</b>
    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.]]>
</string>

But when I try to compile it shows

<string name="lbl_explicacao">
        &lt;b> bla bla bla &lt;/b>

      ....
    </string>
    
asked by anonymous 17.11.2016 / 20:10

1 answer

3

You can use some of the tags HTML to format text used in a TextView.

They can be used in a String Resource or Java using one of the methods fromHTML() of class Html

  • String Resource

    <resources>
        ...
        ...
        <string name="TextoHtml">
            <big>Texto grande</big>\n
            <small>Texto pequeno</small>\n
            <b>Texto em bold</b>\n
            <strike>Texto "riscado"</strike>\n
            Texto<sub>Texto subscrito</sub>\n
            Texto<sup>Texto sobrescrito</sup>\n
            <u>Texto Underline</u>\n
            <font color='red'>Texto em vermelho</font>\n
            <font size='20' color='green'>Texto size e cor</font>
        </string>
    </resources>
    
  • Java

    Spanned textoHTML = Html.fromHtml("<big>Texto grande</big><br/>" +
                                      "<small>Texto pequeno</small><br/>" +
                                      "<b>Texto em bold</b><br/>" +
                                      "<strike>Texto \"riscado\"</strike><br/>" +
                                      "Texto<sub>Texto subscrito</sub><br/>" +
                                      "Texto<sup>Texto sobrescrito</sup><br/>" +
                                      "<u>Texto Underline</u><br/>" +
                                      "<font color='red'>Texto em vermelho</font><br/>" +
                                      "<font size='20' color='green'>Texto size e cor</font>");
    textView.setText(textoHTML);
    
18.11.2016 / 15:02