Html.fromHtml deprecated in Android N
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
Html.fromHtml() is deprecated, what is the alternative?
Either:
Use
Html.fromHtml(String)
on all API levels, or,Use
Html.fromHtml(String)
on API Level 23 and older devices, andHtml.fromHtml(String, int)
on API Level 24+ devices, usingBuild.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.
Android: remove deprecation warning for Html.fromHtml
Well, the one-parameter fromHtml()
is deprecated. The Build
checks ensure that you will not call it on older devices, but it does not change the fact that it is deprecated with a compileSdkVersion
of 24.
You have four choices:
Drop your
compileSdkVersion
below 24. This has rippling effects (e.g., you cannot use 24.x.x versions of the support libraries) and is not a great option.Set your
minSdkVersion
to 24 and get rid of the one-parameterfromHtml()
call. This is impractical in 2016.Live with the strikethrough and Lint complaints.
Add the appropriate
@SuppressLint
annotation to the method, to get the IDE to stop complaining. As Ahlem Jarrar notes, the simplest way to add this is via the quick-fix.
Html.fromHtml deprecated when setting action bar title colour
If your minSdkVersion
is 24 or higher, use the version of fromHtml()
that takes some flags as a parameter. AFAIK, FROM_HTML_MODE_LEGACY
would be the flag value to use for compatibility with the older flag-less fromHtml()
.
If your minSdkVersion
is lower than 24, your choices are:
Always use the
fromHtml()
you are, possibly using the quick-fix (Alt-Enter) to suppress the Lint warningUse both versions of
fromHtml()
: the one taking the flags if your app is running on an API Level 24+ device, or the one without the flags on older devices
The latter would look like:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) {
actionBar.setTitle(Html.fromHtml(..., Html.FROM_HTML_MODE_LEGACY));
}
else {
actionBar.setTitle(Html.fromHtml(...));
}
(where ...
is your HTML to convert)
Note, though, that if you are simply trying to change the color of the entire action bar title, use Sandro Machado's solution. Html.fromHtml()
and similar Spanned
-based solutions are for cases where you need different colors for different pieces of text within a single TextView
(or something using a TextView
, such as the action bar).
how to remove fromHtml deprecated worning using Annotation
You can suppress warning using Annotation. Just add @SuppressWarnings("deprecation")
instead of @SuppressWarnings("deprecated")
, and you won't see that warning anymore.
How to display HTML in TextView?
You need to use Html.fromHtml()
to use HTML in your XML Strings. Simply referencing a String with HTML in your layout XML will not work.
This is what you should do in Java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>", Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
}
And in Kotlin:
textView.text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
Html.fromHtml(html)
}
Android: Html.fromHtml() method not working with standard Html tags
Html class provides limited to support of HTML elements. And "class" attributes are not supported.
To understand which tags can be used and how they will be rendered refer to source code (android 5.0.1 r1). You can provide custom TagHandler or use WebView to display complex HTML page.
Does the Html.fromHtml in android only works in displayed texts?
i think you should remove " ' " before you compare
String str = etU1.getText().toString().replaceAll("[']"," ");
String strTwo = cquestion.get(0).getAnswer().replaceAll("[']"," ");
if(str.equals(strTwo)){
////// do your logic
}
Html.fromHtml on Nougat doesn't call custom HtmlHandler
Html.fromHtml()
will only invoke your TagHandler
for HTML tags that fromHtml()
does not recognize. In your sample HTML, you have:
<p>
<strong>
<u>
<span>
and in your first comment, you also mention div
.
Of those, fromHtml()
has handled <p>
, <strong>
, and <u>
since at least 2010, if not earlier. fromHtml()
in Android 6.0 also handles <div>
(see lines 488-489 in the source), and I forget how back that support goes. Your TagHandler
would not be called for any of these tags, and that behavior is not especially new.
Android 7.0 added support for <span>
(see lines 804-805 from the 7.1 source), and so code that expected a TagHandler
to be invoked for <span>
would behave differently between Android 7.0 and previous versions.
In general, the list of supported tags is undocumented. Google is welcome to change the roster of supported tags at any point.
Your options are:
Live with it
Grab the source to some
Html.java
that you like, refactor it into your own package, and use that copy, modifying it as you see fitFind some other HTML-to-
Spannable
source code that you like better
Related Topics
How to Read a Text File from the Sd Card in Android
Error Inflating Class Android.Support.Design.Widget.Navigationview
How to Pass a Value from One Activity to Another in Android
Disable Scrolling in All Mobile Devices
Android Deprecated Apache Module (Httpclient, Httpresponse, etc.)
Wait Firebase Async Retrieve Data in Android
Difference and Uses of Oncreate(), Oncreateview() and Onactivitycreated() in Fragments
How to Check If Location Services Are Enabled
Android Material and Appcompat Manifest Merger Failed
Android - How to Disable the Click of Home Button
How to Simulate a Touch Event in Android
How to Programmatically Move, Copy and Delete Files and Directories on Sd
Bad Image Quality After Resizing/Scaling Bitmap
How to Disable Recyclerview Scrolling