fromHtml: Different behaviour on JellyBean and Kit

2019-08-02 18:13发布

I am using fromHtml to display formatted text (bold italic etc) in TextView. However, I found it's behaviour is different on JellyBean (4.1.2) and KitKat(4.4.2)

Here is code:

    String myHtml = "<b>hello</b>&#128516;";
    Spanned spanned = Html.fromHtml(myHtml, null, null);

Here html string has &#128516; which is unicode for an emoji. Now after calling fromHtml it returns following value on KitKat (and above):

spanned = hello                

2条回答
来,给爷笑一个
2楼-- · 2019-08-02 18:36

add this java file in your src and add this font .ttf file in assets dir

now use this like below

String myHtml = "<b>hello</b>&#128516;";
Spanned spanned = AndroidEmoji.ensure(myHtml);

for more check here gitcode.

查看更多
SAY GOODBYE
3楼-- · 2019-08-02 18:42

Surprisingly, root of this problem was in Html.toHtml which I had used to convert text in TextView to html. I used custom toHtmland this problem solved. I used .toHtmlwritten in this answer. Indeed it is great solution. I wonder how Android's original Html.toHtml is so lame and defective.

查看更多
登录 后发表回答