Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.fromHtml() is deprecated, what is the alternative? [duplicate]

I updated to the SDK Version 24 and now Html.fromHtml() is deprecated. And the Html class has a a new method with extra parameter named flag, but it's minimum API is 24.

Is there any alternative to this function to the lower API versions?. I don't want to use a WebView for this purpose.

like image 903
Aawaz Gyawali Avatar asked Jun 18 '16 17:06

Aawaz Gyawali


2 Answers

Either:

  • Use Html.fromHtml(String) on all API levels, or,

  • Use Html.fromHtml(String) on API Level 23 and older devices, and Html.fromHtml(String, int) on API Level 24+ devices, using Build.VERSION.SDK_INT to find out the API level of the device that you are running on

In this case, "deprecated" is a hint to go look for the two-parameter method, but the one-parameter method still works and (in all likelihood) will do so for quite some time.

like image 50
CommonsWare Avatar answered Sep 22 '22 19:09

CommonsWare


Just use

 if (Build.VERSION.SDK_INT >= 24) {      Html.fromHtml(String, int) // for 24 api and more  } else {      Html.fromHtml(String) // or for older api  } 

to use Html.fromHtml(String, int) for 24 api follow documentation:

https://developer.android.com/reference/android/text/Html.html

like image 42
Ognev Zair Avatar answered Sep 19 '22 19:09

Ognev Zair