3

I am trying to display, with enhancements, a text taken from inside a pair of tags in an XML file. Here are the opening tag, the text, and the closing tag:

<english><strong>English adjectives: a young woman</strong></br> They are always invariable in gender and number.</english>

But instead of viewing the enhanced text, all I see is exactly the above text between the <english> and </english> tags, with the things like <strong> included. How can I solve this?

Here is the method to read the XML file:

public void lireXML(String mot)
        throws XmlPullParserException, IOException
{

    final TextView monMessage = (TextView) findViewById(R.id.zone_trado_scrollable);

    XmlResourceParser monFichierXML = getResources().getXml(R.xml.text);

    monFichierXML.next();

    int eventType = monFichierXML.getEventType();

    while (eventType != XmlPullParser.END_DOCUMENT)
    {
        if( (eventType == XmlPullParser.START_TAG) && (monFichierXML.getName().equalsIgnoreCase("word"))  )
        {
            monFichierXML.next();
            if ( monFichierXML.getText().equalsIgnoreCase(mot) )
            {

                monFichierXML.nextTag(); // word fermant
                monFichierXML.nextTag(); // english ouvrant
                monFichierXML.next();    // texte anglais

                if (langueChoisie == "francais") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                }

                if (langueChoisie == "espanol") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                    monFichierXML.nextTag(); // français fermant
                    monFichierXML.nextTag(); // espanol ouvrant
                    monFichierXML.next();    // texte espanol
                }

                if (langueChoisie == "chinois") {
                    monFichierXML.nextTag(); // english fermant
                    monFichierXML.nextTag(); // français ouvrant
                    monFichierXML.next();    // texte français
                    monFichierXML.nextTag(); // français fermant
                    monFichierXML.nextTag(); // espanol ouvrant
                    monFichierXML.next();    // texte espanol
                    monFichierXML.nextTag(); // espagnol fermant
                    monFichierXML.nextTag(); // russe ouvrant
                    monFichierXML.next();    // texte russe
                    monFichierXML.nextTag(); // russe fermant
                    monFichierXML.nextTag(); // chinois ouvrant
                    monFichierXML.next();    // texte chinois
                }

                // on affiche le texte à l'intérieur de la balise sélectionnée
                monMessage.setText(monFichierXML.getText());
            }
        }
        eventType = monFichierXML.next();
    }
}`

And here is a bit of XML:

<group>
    <word>young1</word>
    <english><strong>English adjectives: a young woman</strong> <br>They are always invariable in gender and number.</english>
    <francais><strong>Les adjectifs: a young woman</strong> <br>Les adjectifs qualificatifs anglais sont invariables. Quand ils sont épithètes, ils se placent avant le substantif: a young woman. Quand ils sont attributs, ils se placent après le verbe: the woman is young.</francais>
    <espanol><strong>Los adjetivos: a young woman</strong> <br>Los adjetivos en inglés son invariables. Cuando son calificativos, se colocan delante del sustantivo: a young woman. Cuando son atributos, se colocan después del verbo: the woman is young.</espanol>
    <chinois><strong>英语形容词:一位年轻的女士。</strong> <br>不论性别和数量都一样</chinois>
</group>`
TRiG
  • 10,148
  • 7
  • 57
  • 107
Andy
  • 99
  • 1
  • 2
  • 9

3 Answers3

3

If you want to use html tags with a TextView you should use:

 monMessage.setText(Html.fromHtml(monFichierXML.getText()));
SuperFrog
  • 7,631
  • 9
  • 51
  • 81
  • It seems to me, this will be an entirely different architecture. Is it possible while still reading xml? – Andy Sep 28 '15 at 14:12
  • The xml is not directly relevant, you just put the html inside Html.fromHtml('your html'). It doesn't matter where you got the html from. – SuperFrog Sep 28 '15 at 14:13
  • The function fromHtml is now deprecated (Android > N). You should look at https://stackoverflow.com/questions/37904739/html-fromhtml-deprecated-in-android-n – Rémi P Aug 25 '17 at 07:37
2

try something like this

TextView myTextview;

myTextview= (TextView) findViewById(R.id.my_text_view);

htmltext = <your html (markup) character>;

Spanned sp = Html.fromHtml( htmltext );

myTextview.setText(sp);
Shishram
  • 1,514
  • 11
  • 20
0

Try the following:

<b>English adjectives: a young woman</b> <br>They are always invariable in gender and number.

If this fails just encode the < character as &lt;, resulting in:

&lt;b>English adjectives: a young woman&lt;/b> <br>They are always invariable in gender and number.

Check this answer for more information Bold words in a string of strings.xml in Android

Community
  • 1
  • 1
Edson Menegatti
  • 4,006
  • 2
  • 25
  • 40