Aldasa Aldasa - 3 months ago 806
Android Question

Html.fromHtml deprecated in Android N

I am using

Html.fromHtml
to view html in a
TextView
.

Spanned result = Html.fromHtml(mNews.getTitle());
...
...
mNewsTitle.setText(result);


But
Html.fromHtml
is now deprecated in Android N+

What/How do I find the new way of doing this?

Answer

You have to add a version check and use the old method on Android M and below, on Android N and higher you should use the new method. If you don't add a version check your app will break on lower Android versions.

Spanned result;

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
    result = Html.fromHtml(mNews.getTitle(),Html.FROM_HTML_MODE_LEGACY);
} else {
    result = Html.fromHtml(mNews.getTitle());
}
Comments