How to format text in a Textview?

Asked

Viewed 2,307 times

4

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

I have string in values:

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

There’s such a thing Html.fromHtml(String, flags), but I don’t know how to use

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

I tried to use in 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>
  • it really needs to be in a Textview, because if you go through a webview you have more possibilities for formatting?

  • Because I think it would appear as a web page (I think), if it appears as a Textview, I can try

  • Voce can configure to look like you want, can by a white background, image background, at last, is a mini browser that you can use jvascripts, can use css and much more, see a my application using webview: https://play.google.com/store/apps/details?id=com.armando.raonimotores.ossonhosdeaaz. on it, the top is java, the bottom is a webview

  • I’m searching how to use it. Because I actually already have a predefined text in the strings.xml file. I would like it to be displayed

  • i tried to use in string <string name="lbl_explanation"> <![CDATA[]]>

  • You should not change the question this way, now my answer has become meaningless. I will reverse the edition.

  • Blz. It’s just that I’m still learning to hit you with this technology.. Sorry

Show 2 more comments

1 answer

3


It is possible to use some of the tags HTML to format text used in a Textview.

They can be used in a String Resource or in Java using one of the methods fromHTML() 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);
    
  • But when trying to emulate it gives an error in Resource: When trying to load the emulator, it gives the following error: Error:(357) Multiple Substitutions specified in non-positional format; Did you Mean to add the Formatted="false" attribute? Error:(381) Unexpected end tag string and in Html.fromHtml it skins a String and an Html flag.fromHtml(String, flag), if not put it said it is deprecated.. How to solve this?

  • No mistake if you do as it is in the reply. Do not call deprecated if minSdkVersion is below API level 24.

  • really, it worked out! Thank you very much! So let me see if I got it, the text if it’s great doesn’t have to have space between the lines?

  • I don’t know what you mean.

  • Every time I gave <enter> and started another line and when I tried to compile it gave error

  • You gotta do it like this: "texto1" + "Texto2", where is the + can do <enter>

  • This in java right? Gave error in string.xml

  • If it’s not too much. There’s how I leave a <string name="field">value</string> waiting for a value to be defined in java and displayed later?

  • It exists, but it depends on how you want to use it. Open another question and explain what you intend to do, but first see this

  • excellent! Mutio good

  • Boy didn’t work with my text, just as your example, there is some character limitation?

  • I can send you my text to see where I’m going wrong?

  • If you are trying to use replacement(placeholders) ask another question if you do not edit this question and put the text there.

  • Ready. I changed the code

  • The problem is in the use of the symbol % use the attribute formatted="false" thus <string name="lbl_explicacao" formatted="false">

  • was just that.. exactly that... then I discovered that to compile with this symbol I would have to put %. Quite right.. Thank you very much

  • There are situations where %% results, others where both appear, but I don’t remember when

  • 1

    If I put: "Text % ." it works, now if I put Text 10% would give error

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.