Daniele B Daniele B - 1 year ago 205
Android Question

Android: remove deprecation warning for Html.fromHtml

I would like to remove the deprecation warning for


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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download