Html.fromHtml deprecated in Android N

186,572

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

FROM_HTML FLAGS

Solution 4

Or you can use androidx.core.text.HtmlCompat:

HtmlCompat.fromHtml("<b>HTML</b>", HtmlCompat.FROM_HTML_MODE_LEGACY)

HtmlCompat docs

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()
Share:
186,572
Aldasa
Author by

Aldasa

I only wrote this for the badge :(

Updated on July 15, 2022

Comments

  • Aldasa
    Aldasa almost 2 years

    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?

  • ban-geoengineering
    ban-geoengineering about 8 years
    Which flag does the zero represent?
  • ban-geoengineering
    ban-geoengineering about 8 years
    Html.FROM_HTML_MODE_LEGACY
  • vanomart
    vanomart almost 8 years
    ah, waiting for something like HtmlCompat to appear
  • Ted Hopp
    Ted Hopp almost 8 years
    It's also useful to add a //noinspection deprecation comment just under the else to avoid lint warnings.
  • Yair Kukielka
    Yair Kukielka almost 8 years
    You can see what each of these flags do in this blog post: medium.com/@yair.kukielka/…
  • Stoycho Andreev
    Stoycho Andreev over 7 years
    Same effect like the accepted answer, but +1 because of SuppressWarnings annotation
  • Kalpesh Patel
    Kalpesh Patel over 7 years
    Can you please share input HTML also? This would help in better understanding the conversion.
  • Ranjithkumar
    Ranjithkumar over 7 years
    Can you give small explanation about this mode?
  • Ranjithkumar
    Ranjithkumar over 7 years
    Can you give small explanation about this mode?
  • Christine
    Christine about 7 years
    I see that the style attributes are not implemented, is there a way to implement them?
  • flo1411
    flo1411 about 7 years
    And this seems to be deprecated as well and can be replaced by Html.FromHtml(htmlText, Android.Text.FromHtmlOptions.ModeLegacy);
  • JJD
    JJD about 7 years
    When I use your library on an app which uses minSdkVersion 15 and targetSdkVersion 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.Co‌​lored'. Your library targets API level 25, obviously. How can still I use it?
  • Minami
    Minami over 6 years
    you may save typings by remove Spanned and return
  • android developer
    android developer over 6 years
    Why 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
    shareef almost 6 years
    could you provide all HtmlCompact may be on git hub it looks cool
  • Wajid
    Wajid over 5 years
    if you want SDK to handle version checks, use: HtmlCompat.fromHtml("textWithHtmlTags", HtmlCompat.FROM_HTML_MODE_LEGACY)
  • k2col
    k2col over 5 years
    @shareef I would but it's really just a boring utility class with this single method in it....
  • Leonardo Sibela
    Leonardo Sibela about 4 years
    For me @SuppressWarnings("deprecation") did not work. I used @Suppress("deprecation") instead.
  • xjcl
    xjcl about 4 years
    @vanomart Aaaand HtmlCompat including HtmlCompat.fromHtml is a thing now =)
  • CDrosos
    CDrosos almost 4 years
    you can use this even on Android 5?
  • Ondřej Z
    Ondřej Z almost 4 years
    @CDrosos yes, it is part of support library
  • Amir Dora.
    Amir Dora. almost 3 years
    you don't need to add the else part, fromHtml method handles it by default for lower api devices
  • James
    James almost 3 years
    You can replace the if/else with: HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY)
  • Ali Kazi
    Ali Kazi over 2 years
    You can also just use HtmlCompat and avoid checking Android version.
  • user924
    user924 over 2 years
    why not FROM_HTML_MODE_COMPACT?