The solutions in the answer linked to by @sparkymat generally require either regex - which is an error-prone approach - or installing a third-party library such as jsoup or jericho. A better solution on Android devices is just to make use of the Html.fromHtml() function:
public String stripHtml(String html) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY).toString();
} else {
return Html.fromHtml(html).toString();
}
}
This uses Android's built in Html parser to build a Spanned representation of the input html without any html tags. The "Span" markup is then stripped by converting the output back into a string.
As discussed here, Html.fromHtml behaviour has changed since Android N. See the documentation for more info.
You can alternatively use Html.escapeHtml(String) if you are targeting API 16 or above.
For also targeting below API 16, you can instead use the below class by calling HtmlUtils.escapeHtml(String) which i simply pulled from the source of Html.escapeHtml(String).
public class HtmlUtils {
public static String escapeHtml(CharSequence text) {
StringBuilder out = new StringBuilder();
withinStyle(out, text, 0, text.length());
return out.toString();
}
private static void withinStyle(StringBuilder out, CharSequence text,
int start, int end) {
for (int i = start; i < end; i++) {
char c = text.charAt(i);
if (c == '<') {
out.append("<");
} else if (c == '>') {
out.append(">");
} else if (c == '&') {
out.append("&");
} else if (c >= 0xD800 && c <= 0xDFFF) {
if (c < 0xDC00 && i + 1 < end) {
char d = text.charAt(i + 1);
if (d >= 0xDC00 && d <= 0xDFFF) {
i++;
int codepoint = 0x010000 | (int) c - 0xD800 << 10 | (int) d - 0xDC00;
out.append("&#").append(codepoint).append(";");
}
}
} else if (c > 0x7E || c < ' ') {
out.append("&#").append((int) c).append(";");
} else if (c == ' ') {
while (i + 1 < end && text.charAt(i + 1) == ' ') {
out.append(" ");
i++;
}
out.append(' ');
} else {
out.append(c);
}
}
}
}
The solutions in the answer linked to by @sparkymat generally require either regex - which is an error-prone approach - or installing a third-party library such as jsoup or jericho. A better solution on Android devices is just to make use of the Html.fromHtml() function:
This uses Android's built in Html parser to build a
Spanned
representation of the input html without any html tags. The "Span" markup is then stripped by converting the output back into a string.As discussed here, Html.fromHtml behaviour has changed since Android N. See the documentation for more info.
This is for new method alternative (API 16+):
Html.fromHtml can be extremely slow for large html strings.
Here's how you can do it, easily and fast with jsoup:
Add this line to your gradle file:
Check what is the latest jsoup version here: https://jsoup.org/download
Add this line to your code:
Check this link here to learn how to preserve line breaks:
How do I preserve line breaks when using jsoup to convert html to plain text?
This is dead simple with jsoup
You can alternatively use
Html.escapeHtml(String)
if you are targeting API 16 or above.For also targeting below API 16, you can instead use the below class by calling
HtmlUtils.escapeHtml(String)
which i simply pulled from the source ofHtml.escapeHtml(String)
.I am using this class which works fine.
Sorry for the late post, but i think this might help for others,
To just remove the html strips
This way the html tag will be replaced with string, but the string willnot be formatted properly. Hence i did
This way i first replace with nextline with blankspace and removed blank space. Similarly you can remove others.