Android Html.fromHtml () does not work correctly with nested text style
I want to display HTML
"<html><body><font color=\"black\"> In the name of <font color=\"blue\"><b> God</b></font> the compassionate, the merciful</font></body><html>" in a textview. So I used Html.fromHtml() , but the output is different compared to browsers.
The difference is that the word "God" is not displayed in blue in the TextView.
Code:
textView.setText("<html><body><font color=\"black\"> In the name of <font color=\"blue\"><b> God</b></font> the compassionate, the merciful</font></body><html>"); Html.fromHtml (String) does not support all HTML tags
List of valid HTML tags:
- br
- R
- Div
- Em
- b
- strong
- I am
- Doctor of Philosophy
- large
- little
- font
- blockquote
- tt
- a
- sup
- sub
Try it like this:
textView.setText(Html.fromHtml("<html><body>In the name of<font color=blue><b> God </b></font>the compassionate, the merciful</body><html>")); Note. Black is the default color, so you don't need to use the
<font>for black.
if you want to use the full tags correctly, for example (for example, for GRAY and BLUE ):
textView.setText(Html.fromHtml("<html><body><font color=gray>In the name of </font><font color=blue><b> God </b></font><font color=gray>the compassionate, the merciful</font></body><html>")); I just ran into the same problem. This seems to be a bug in the implementation of the Html for the Android SDK. For the simplest case:
<font color=green>Lorem</font color=green> Ipsum The text is displayed in green, i.e. The color tag of the final font is ignored. This seems to be related to the implementation of Html.endFont:
private static void endFont(Editable text) { Font font = getLast(text, Font.class); if (font != null) { setSpanFromMark(text, font, new TypefaceSpan(font.mFace)); } The code tries to find the range that was started with the Font type (by calling getLast ()); but the original spacing was started as a foreground spacing, and not as a font spacing. Thus, it never finds the original start interval (font == null above) and therefore never ends this interval. It seems like the code for endFont should determine what type of font covers the range of the end tag, and then look for it, not just Font.class.