HTML.fromHtml adds space at end of text?

Viewed 5393

In my app I use the Html.fromHtml(string).toString method to remove some <p> tags that are received when I parse some JSON.

If I leave the <p> tags on, the text fits the background perfectly (the background is a relative layout with wrap_content in both height and width.) However, if I use fromHtml to remove the <p> tags, suddenely there is a huge space below the text, which I believe is the fromHtml method adding in space at the end?

Any ideas?

EDIT:

Here are screenshots: http://imgur.com/a/zIZNo

The one with <p> tags is the one that doesnt use fromHtml, obviously! :)

EDIT 2: Solution has been found, see my answer below. Thank you to Andro Selva for helping me by telling me about the hidden /n that was being added!

5 Answers

This solution works for me Create a helper method to replace all paragraph starting and ending tags and replace all with empty characters.

@Nullable
public static String removeParagraphTags(@Nullable String input) {
    if (input == null) {
        return null;
    }
    return input.replaceAll("<p>", "").replaceAll("</p>", "");
}

And Usage

  String input = "<p>This is some text in a paragraph.</p>";
HtmlCompat.fromHtml(StringUtils.removeParagraphTags(input),HtmlCompat.FROM_HTML_MODE_COMPACT)
Related