2

I have a custom TextView, to show html text. For pre-Nougat devices it works. As you already know on Nougat, fromHtml is deprecated and it needs a flag..so my code is like this

 Spannable s = getRichText(text);
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
     super.setText(Html.fromHtml(s.toString(), Html.FROM_HTML_MODE_COMPACT, this, new HtmlHandler(getContext())), BufferType.SPANNABLE);
 } else {
     super.setText(Html.fromHtml(s.toString(), this, new HtmlHandler(getContext())), BufferType.SPANNABLE);
 }

The problem it that, the HtmlHandler class is never get called. (I have already tried all the flags). On HtmlHandler I handle tags and styles, eg background-color, color et cetera. I have implemented to get colors from rgb, rgba, hls etc. But on Nougat it accepts only colors with hex, because on Nougat, fromHTML can "read" colors and show them. Why is this happening? How can I keep my way for the colors? If you didn't understand something, or need more details, let me know.

The html I use for testing is

<p><strong>Server</strong><u> message</u><strong><u>!!!</strong></u> <span style="background-color: rgb(255,0,0);">Not working on Nugat</span></p>
Vasileios Pallas
  • 4,801
  • 4
  • 33
  • 50
  • Could you edit your question and provide a sample of the HTML, plus an indication of what out of that is not being routed to your `TagHandler` (where it used to pre-7.0)? For example, `Html` has supported `` for quite some time. I see that 7.1 (and presumably 7.0) now handles `` tags, whereas it did not do so before -- is that where your problem lies? – CommonsWare Oct 23 '16 at 20:49
  • I edit the question. The problem isn't the `span`. Because It doesn't work neither with `div` nor with `p`. The problem is that my `HtmlHandler` class never gets called. That's the weird part – Vasileios Pallas Oct 23 '16 at 21:03
  • In my TagHandler I check for `ol`, `ul`, `strike`, `code`, `span`, `div` and for css styles for `background-color`, `color` and for fontawesome, and non of them is working on Nougat – Vasileios Pallas Oct 23 '16 at 21:08

1 Answers1

5

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 fit

  • Find some other HTML-to-Spannable source code that you like better

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