74

I need to decode HTML entities, e.g. from ö to ö, and & to &.

URLEncoder.decode(str) does not do the job (convert from % notations). TextUtils has a HTMLencode, but not a HTMLdecode.

Are there any function for decoding HTML entities?

Brais Gabin
  • 5,827
  • 6
  • 57
  • 92
johboh
  • 1,003
  • 2
  • 9
  • 10

6 Answers6

119

The Html class is supposed to do that, however it is said that everything is not supported. It always worked for me but I never had ö so I can't tell for this one. Try Html.fromHtml(yourStr) to get the decoded string.

Majid
  • 13,853
  • 15
  • 77
  • 113
Sephy
  • 50,022
  • 30
  • 123
  • 131
27

Html.fromHtml(String html) is deprecated after API v24 so this is the correct way to do it

  if (Build.VERSION.SDK_INT >= 24)
  {
       textView.setText(Html.fromHtml(htmlString , Html.FROM_HTML_MODE_LEGACY)));  
  }
  else
  {
       textView.setText(Html.fromHtml(htmlString));
  }
Aalok
  • 553
  • 8
  • 21
14

Simply you can do that using this code

  Html.fromHtml(String).toString();

Hope this will help you

saeed
  • 1,935
  • 1
  • 18
  • 33
2

you can use WebView to represent any html text easily by following below steps.

first convert your data in html format as :

String res=null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
res=Html.fromHtml(product.getDescription(),Html.FROM_HTML_MODE_COMPACT).toString();
}
else{
res=Html.fromHtml(product.getDescription()).toString();
}

Then load your data in WebView as:

myWebView.loadDataWithBaseURL(null, res, "text/html", "utf-8", null);
Ankit Dubey
  • 1,000
  • 1
  • 8
  • 12
1

You can remove special char from string by calling

responsestring.replace(“special char here”, “”);

you can convert response into a string from htmlstring like that – Html.fromHtml( response string here ) But this method is depreciated on API 24 so you need to do it in a correct way-

if (Build.VERSION.SDK_INT >= 24)
{
    post_description.setText(Html.fromHtml( response here , Html.FROM_HTML_MODE_LEGACY));
}
else
{
    post_description.setText(Html.fromHtml( response here ));
}
Kamal Bunkar
  • 1,354
  • 1
  • 16
  • 20
0

To work around the deprecation in API v24 you can use

HtmlCompat.fromHtml(htmlString, HtmlCompat.FROM_HTML_MODE_COMPACT).toString()
StefanTo
  • 971
  • 1
  • 10
  • 28