fromHtml: Different behaviour on JellyBean and KitKat (and above)
Asked Answered
S

2

1

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😄

Here is screenshot of Android Studio for the same:

enter image description here

This is expected behaviour as we can see corresponding emoji in spanned.

But on JellyBean the same call returns following value:

spanned = hello�� 

Here is screenshot:

enter image description here

This is indeed not expected and driving me nuts. I don't know what I am doing wrong. If anyone having idea please can you help?

Shaughnessy answered 14/3, 2018 at 5:26 Comment(0)
S
0

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.

Shaughnessy answered 24/3, 2018 at 14:13 Comment(0)
B
0

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.

Basketwork answered 14/3, 2018 at 5:35 Comment(1)
Thanks for answer but this is for Android version below 4.1.1. Mine is JellyBean (4.1.2) so it is not working for me :( – Shaughnessy
S
0

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.

Shaughnessy answered 24/3, 2018 at 14:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.