Skip to content Skip to sidebar Skip to footer

How To Decode Html And Escape Characters In Textview Android

I have specific data that contain some html characters such as < for < > for > & for & And some escape characters like this \r\n\r\n, W

Solution 1:

Html.fromHtml(str).toString(); should work. I tell you the reason -

Your string is Html. Unicode characters are stored as encoded character entities. The &#x; notation is used to escape unicode characters for transmission over ISO-8859-1. A web browser decodes them to display actual unicode characters.

Decoding HTML is decoding HTML entities to Java raw unicode characters.

For example:

String html = "B &#x0026; This is HTML";
String java = Html.fromHtml(html);
#=> Output: "B \u0026 This is HTML"String strJava = Html.fromHtml(html).toString();
#=> Output: "B & This is HTML"

Solution 2:

You have to use Html.fromHtml() method. This method returns displayable styled text from the provided HTML string.

Below API Level 23 (Before Nougat):

textView.setText(Html.fromHtml("<h2>Html String</h2>"));

From API Level 23 (From nougat):

textView.setText(Html.fromHtml("<h2>Html String</h2>", Html.FROM_HTML_MODE_COMPACT));

Post a Comment for "How To Decode Html And Escape Characters In Textview Android"