Bilbo Beggins
Bilbo Beggins

Reputation: 176

HTML Tag parse error in Android 2.3.3

I use this code:

alertDialog.setMessage(Html.fromHtml(getString(R.string.text));

And the string text contains:

<strong>Hello StackOverflow!</strong>, <em>today it's a beatiful day!</em>.

It works like a charm in Android 4.0.3! However, in Android 2.3.3, the tags are inverted! The <strong> tag makes the text italic, and the <em> tag makes the text bold!!

I have no idea why. The LogCat doesn't report anything!

Upvotes: 2

Views: 304

Answers (2)

Lukas Knuth
Lukas Knuth

Reputation: 25755

This seems to be is a bug. Have a look at the sources of the HTML-class for Android 2.2 (not yours, but close):

private void handleStartTag(String tag, Attributes attributes) {
    //... Other if's
    } else if (tag.equalsIgnoreCase("b")) {
        start(mSpannableStringBuilder, new Bold());
    } else if (tag.equalsIgnoreCase("strong")) {
        start(mSpannableStringBuilder, new Italic()); // <-- PROBLEM
    }
    // ... More here
}

This has been fixed in the Android 4.2.2 sources:

private void handleStartTag(String tag, Attributes attributes) {
    // Others up here...
    else if (tag.equalsIgnoreCase("strong")) {
        start(mSpannableStringBuilder, new Bold()); // <- FIXED
    } else if (tag.equalsIgnoreCase("b")) {
        start(mSpannableStringBuilder, new Bold());
    }
    // More down here...
}

Solution: As you can see, the <b>-tag works as it should. You can use it instead.

Upvotes: 4

Neil Townsend
Neil Townsend

Reputation: 6084

This is a known issue: https://code.google.com/p/android/issues/detail?id=3473.

From that page, it seems that the solution is to use <b> and <i> rather than <strong> and <em> because the former are defined to be bold and italic whilst the second pair are implementation dependent.

Upvotes: 1

Related Questions