3

I would like to remove the deprecation warning for Html.fromHtml(string).

I tried to do like this:

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
        htmlSpanned = Html.fromHtml(string,Html.FROM_HTML_MODE_LEGACY);
    } else {
        //noinspection deprecation
        htmlSpanned = Html.fromHtml(string);
    }

but it still gives me a warning during compilation:

Warning:(18, 31) [deprecation] fromHtml(String) in Html has been deprecated

Daniele B
  • 19,801
  • 29
  • 115
  • 173

5 Answers5

4

If your minSdkVersion is 24 or higher, use the version of fromHtml() that takes some flags as a parameter . AFAIK, FROM_HTML_MODE_LEGACY would be the flag value to use for compatibility with the older flag-less fromHtml().

If your minSdkVersion is lower than 24, your choices are:

Always use the fromHtml() you are, possibly using the quick-fix (Alt-Enter) to suppress the Lint warning

Use both versions of fromHtml(): the one taking the flags if your app is running on an API Level 24+ device, or the one without the flags on older devices.

Ahlem Jarrar
  • 1,129
  • 1
  • 12
  • 33
4

Well, the one-parameter fromHtml() is deprecated. The Build checks ensure that you will not call it on older devices, but it does not change the fact that it is deprecated with a compileSdkVersion of 24.

You have four choices:

  1. Drop your compileSdkVersion below 24. This has rippling effects (e.g., you cannot use 24.x.x versions of the support libraries) and is not a great option.

  2. Set your minSdkVersion to 24 and get rid of the one-parameter fromHtml() call. This is impractical in 2016.

  3. Live with the strikethrough and Lint complaints.

  4. Add the appropriate @SuppressLint annotation to the method, to get the IDE to stop complaining. As Ahlem Jarrar notes, the simplest way to add this is via the quick-fix.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

This was my solution at one point:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    textField.setText(Html.fromHtml(htmlText, Html.FROM_HTML_MODE_COMPACT));
} else {
    textField.setText(Html.fromHtml(htmlText);
}

It worked just fine.

zeroDivider
  • 1,050
  • 13
  • 29
0

You can try it. I mean your code looks well, I tried it and it worked for me.

// get our html content

String htmlAsString = getString(R.string.html);
Spanned htmlAsSpanned = Html.fromHtml(htmlAsString);

// used by TextView

// set the html content on the TextView

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(htmlAsSpanned);

Our you can try it:

@SuppressWarnings("deprecation")
Cristofer
  • 1,046
  • 13
  • 21
0

Android or Kotlin or Google has created HtmlCompat which can be used instead of the method Html. Add this dependency implementation 'androidx.core:core:1.0.1' to the build.gradle file of your app. Make sure you use the latest version of androidx.core:core.

This allows you to use:

 HtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);

You can convert the HTML.FROM_HTML_MODE_LEGACY into an additional parameter if you want. This gives you more control about it which flag to use.

You can read more about the different flags on the HTML Class documentation

Nitin Karande
  • 1,280
  • 14
  • 33