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.


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.


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


Actually there is another method with flag parameter

/** @deprecated */
@Deprecated
public static Spanned fromHtml(String source) {
    throw new RuntimeException("Stub!");
}

public static Spanned fromHtml(String source, int flags) {
    throw new RuntimeException("Stub!");
}

just use fromHtml function with flag parameter. flag parameters are

public static final int FROM_HTML_MODE_COMPACT = 63;
public static final int FROM_HTML_MODE_LEGACY = 0;
public static final int FROM_HTML_OPTION_USE_CSS_COLORS = 256;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE = 32;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_DIV = 16;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_HEADING = 2;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST = 8;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_LIST_ITEM = 4;
public static final int FROM_HTML_SEPARATOR_LINE_BREAK_PARAGRAPH = 1;
public static final int TO_HTML_PARAGRAPH_LINES_CONSECUTIVE = 0;
public static final int TO_HTML_PARAGRAPH_LINES_INDIVIDUAL = 1;