Html.fromHtml deprecated in Android N
Solution 1
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
Solution 2
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);
}
}
Solution 3
Compare of the flags of fromHtml().
<p style="color: blue;">This is a paragraph with a style</p>
<h4>Heading H4</h4>
<ul>
<li style="color: yellow;">
<font color=\'#FF8000\'>li orange element</font>
</li>
<li>li #2 element</li>
</ul>
<blockquote>This is a blockquote</blockquote>
Text after blockquote
Text before div
<div>This is a div</div>
Text after div
Solution 4
Or you can use androidx.core.text.HtmlCompat
:
HtmlCompat.fromHtml("<b>HTML</b>", HtmlCompat.FROM_HTML_MODE_LEGACY)
Solution 5
If you are lucky enough to develop on Kotlin, just create an extension function:
fun String.toSpanned(): Spanned {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return Html.fromHtml(this, Html.FROM_HTML_MODE_LEGACY)
} else {
@Suppress("DEPRECATION")
return Html.fromHtml(this)
}
}
And then it's so sweet to use it everywhere:
yourTextView.text = anyString.toSpanned()
![Aldasa](https://i.stack.imgur.com/pXpMt.jpg?s=256&g=1)
Comments
-
Aldasa almost 2 years
I am using
Html.fromHtml
to view html in aTextView
.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?
-
ban-geoengineering about 8 yearsWhich flag does the zero represent?
-
ban-geoengineering about 8 years
Html.FROM_HTML_MODE_LEGACY
-
vanomart almost 8 yearsah, waiting for something like HtmlCompat to appear
-
Ted Hopp almost 8 yearsIt's also useful to add a
//noinspection deprecation
comment just under theelse
to avoid lint warnings. -
Yair Kukielka almost 8 yearsYou can see what each of these flags do in this blog post: medium.com/@yair.kukielka/…
-
Stoycho Andreev over 7 yearsSame effect like the accepted answer, but +1 because of SuppressWarnings annotation
-
Kalpesh Patel over 7 yearsCan you please share input HTML also? This would help in better understanding the conversion.
-
Ranjithkumar over 7 yearsCan you give small explanation about this mode?
-
Ranjithkumar over 7 yearsCan you give small explanation about this mode?
-
Christine about 7 yearsI see that the style attributes are not implemented, is there a way to implement them?
-
flo1411 about 7 yearsAnd this seems to be deprecated as well and can be replaced by
Html.FromHtml(htmlText, Android.Text.FromHtmlOptions.ModeLegacy);
-
JJD about 7 yearsWhen I use your library on an app which uses
minSdkVersion 15
andtargetSdkVersion 23
I get a build error for values-v24.xml:Error:(3) Error retrieving parent for item: No resource found that matches the given name 'android:TextAppearance.Material.Widget.Button.Borderless.Colored'.
Your library targets API level 25, obviously. How can still I use it? -
Minami over 6 yearsyou may save typings by remove
Spanned
andreturn
-
android developer over 6 yearsWhy is it deprecated? What's better to use now? Why the need for the function you wrote, if the code behind the deprecated functions already use this flag?
-
shareef almost 6 yearscould you provide all HtmlCompact may be on git hub it looks cool
-
Wajid over 5 yearsif you want SDK to handle version checks, use:
HtmlCompat.fromHtml("textWithHtmlTags", HtmlCompat.FROM_HTML_MODE_LEGACY)
-
k2col over 5 years@shareef I would but it's really just a boring utility class with this single method in it....
-
Leonardo Sibela about 4 yearsFor me @SuppressWarnings("deprecation") did not work. I used @Suppress("deprecation") instead.
-
xjcl about 4 years@vanomart Aaaand
HtmlCompat
includingHtmlCompat.fromHtml
is a thing now =) -
CDrosos almost 4 yearsyou can use this even on Android 5?
-
Ondřej Z almost 4 years@CDrosos yes, it is part of support library
-
Amir Dora. almost 3 yearsyou don't need to add the else part, fromHtml method handles it by default for lower api devices
-
James almost 3 yearsYou can replace the if/else with:
HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
-
Ali Kazi over 2 yearsYou can also just use HtmlCompat and avoid checking Android version.
-
user924 over 2 yearswhy not
FROM_HTML_MODE_COMPACT
?