Obsolete html.fromHtml for Android N+

Asked

Viewed 75 times

3

In Android 7.0 (API level 24) the static method fromHtml() of the public class Html became obsolete I don’t know why, if you know tell me. See an example of how it was used:

tv.setText(Html.fromHtml("<h3>Título</h3><br><p>Aqui é feito a descrição</p>"));

Considering this fact, what would be its equivalent? How can I use the fromHtml() for Android N+?

1 answer

6


In fact Html.fromHtml(String source) is now considered obsolete. This, however, does not imply that it cannot be used. Because there is no other option, in case you want the app to run in versions prior to N.

The method that replaces it is Html.fromHtml(String source, int flag)(1).

To the parameter flag one or more (separated by the operator or |) of the constants that the class Html declaring.

Each of these "flags" indicates how the html in source should be "interpreted" when constructing the Spanned returned by function.

For example, the constant FROM_HTML_SEPARATOR_LINE_BREAK_LIST indicates which text inside elements <ul> are by default separated from other texts by a newline.

(1) - Use the constant Html.FROM_HTML_MODE_LEGACY to obtain the behavior prior to version N.

  • 1

    Very good Ramaral. + 1. I didn’t expect any other answer than this.

  • @ramaral, I would like to replace this obsolete statement with this one you mentioned, but it does not accept null as int flag. I just need to format some links. Can you tell which tag I should use?

  • 1

    @Renefreak Use the constant Html.FROM_HTML_MODE_LEGACY

Browser other questions tagged

You are not signed in. Login or sign up in order to post.