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?
androidandroid-7.0-nougatdeprecated
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?
Best Solution
update: as @Andy mentioned below Google has created
HtmlCompat
which can be used instead of the method below. Add this dependencyimplementation 'androidx.core:core:1.0.1
to the build.gradle file of your app. Make sure you use the latest version ofandroidx.core:core
.This allows you to use:
You can read more about the different flags on the HtmlCompat-documentation
original answer: In Android N they introduced a new
Html.fromHtml
method.Html.fromHtml
now requires an additional parameter, named flags. This flag gives you more control about how your HTML gets displayed.On Android N and above you should use this new method. The older method is deprecated and may be removed in the future Android versions.
You can create your own Util-method which will use the old method on older versions and the newer method on Android N and above. If you don't add a version check your app will break on lower Android versions. You can use this method in your Util class.
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