Subby
Subby

Reputation: 5480

Html.fromHtml shows commented line

Html.fromHtml successfully renders the text, however, the HTML which I have contains a comment:

<!-- p {margin-top:0;margin-bottom:0}-->

And this comment line is showing in my TextView:

enter image description here

How do I prevent the comment line from showing?

Code

public String getFormattedBody()
{
    String formattedContent;

    if (contentType.equalsIgnoreCase("html"))
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            formattedContent = Html.fromHtml(content, Html.FROM_HTML_MODE_LEGACY).toString();
        }
        else
        {
            formattedContent = Html.fromHtml(content).toString();
        }
    }
    else
    {
        return content;
    }

    return formattedContent;
}

Upvotes: 1

Views: 123

Answers (2)

goodhyun
goodhyun

Reputation: 5002

Just brutally get rid of them.

.replaceAll("(?s)<!--.*?-->", "")

Upvotes: 1

Subby
Subby

Reputation: 5480

I used a regular expression to manually remove the comment:

final Pattern pattern = Pattern.compile("<!--(.*?)-->");
final Matcher matcher = pattern.matcher(formattedContent);

while (matcher.find())
{
    final String htmlComment = matcher.group();
    formattedContent = formattedContent.replace(htmlComment, "");
}

Upvotes: 0

Related Questions