47

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.

Oleg Osipenko
  • 2,409
  • 1
  • 20
  • 28
Aawaz Gyawali
  • 3,244
  • 5
  • 28
  • 48

3 Answers3

84

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
58

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

Ognev Zair
  • 1,626
  • 1
  • 13
  • 17
  • 9
    A small preference of mine: `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Html.fromHtml(String, int) // for 24 api and more } else { Html.fromHtml(String) // or for older api }` – Tash Pemhiwa May 10 '17 at 17:25
12

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;
okarakose
  • 3,692
  • 5
  • 24
  • 44
  • That method call requires API 24 above, I found that and edited my question too, – Aawaz Gyawali Jun 18 '16 at 17:47
  • @OmerKarakose the compiler cannot resolve the integer flag. It is expecting this format: fromHtml (String source, int flags, Html.ImageGetter imageGetter, Html.TagHandler tagHandler) – iOSAndroidWindowsMobileAppsDev Feb 15 '17 at 09:10
  • @OmerKarakose error: no suitable method found for fromHtml(String,int) urlBuilder.setText(Html.fromHtml(encodedUrl1, FROM_HTML_MODE_LEGACY)); ^ method Html.fromHtml(String) is not applicable (actual and formal argument lists differ in length) method Html.fromHtml(String,ImageGetter,TagHandler) is not applicable (actual and formal argument lists differ in length) 1 error – iOSAndroidWindowsMobileAppsDev Feb 15 '17 at 09:26
  • @Anon I think you are not using android api level 24 or above. `Html.fromHtml(String source, int flags)` method is coming with **api 24**. – okarakose Feb 15 '17 at 12:55
  • @OmerKarakose My target api in app:build.gradle is 23 Is that the problem? – iOSAndroidWindowsMobileAppsDev Feb 15 '17 at 13:14
  • @Anon yes that is the problem. if you want to build your application with newer api levels, you can upgrade from 23 to 25 for now. and if you do that upgrade, you will see `Html.fromHtml(String source)` function is deprecated. if you will stay at api level 23 or below, you can continue to use `Html.fromHtml(String source)` function. – okarakose Feb 15 '17 at 13:19