Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 212
Aldasa Avatar asked Jun 19 '16 07:06

Aldasa


People also ask

What can I use instead of fromHtml?

fromHtml(text) is deprecated in Android N. Google has created HtmlCompat which can be used instead of the method below.

Is HTML supported in Android?

text. HTML that processes HTML strings into displayable styled text and then we can set the text in our TextView. We can also use WebView for displaying HTML content. Currently Android does not support all the HTML tags but it supports all major tags.

Can we use HTML in Android Studio?

Step 1: To add a local HTML file into your Android project there must be an asset folder in it. To create an asset folder in Android studio open your project in Android mode first as shown in the below image. Step 2: Go to the app > right-click > New > Folder > Asset Folder and create the asset folder.


2 Answers

update: as @Andy mentioned below Google has created HtmlCompat which can be used instead of the method below. 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 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.

@SuppressWarnings("deprecation") public static Spanned fromHtml(String html){     if(html == null){         // return an empty spannable if the html is null         return new SpannableString("");     }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {         // FROM_HTML_MODE_LEGACY is the behaviour that was used for versions below android N         // we are using this flag to give a consistent behaviour         return Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);     } else {         return Html.fromHtml(html);     } } 

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

like image 175
Rockney Avatar answered Sep 21 '22 22:09

Rockney


I had a lot of these warnings and I always use FROM_HTML_MODE_LEGACY so I made a helper class called HtmlCompat containing the following:

   @SuppressWarnings("deprecation")    public static Spanned fromHtml(String source) {         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {             return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);         } else {             return Html.fromHtml(source);         }     } 
like image 35
k2col Avatar answered Sep 21 '22 22:09

k2col