Android issue with rendering emoji

477 views Asked by At

I have textfield that I am setting text string and observing strange issue

If the string is "" which is codepoints 128591 1f64f I see this image1

But if the string is "" which is basically 2 code points with values 128591 128591, I see this image

enter image description here

Is there something special I need to do in order for the first emoji to get rendered correctly in a textView?

The emoji in question is - https://www.unicodepedia.com/unicode/emoticons/1f64f/person-with-folded-hands/

I am using appCompat 1.4.1 and I am use AppCompatTextView to render text.

1

There are 1 answers

0
Subodh Nijsure On

Above issue was because I was setting wrong length on spannable string that had emoji.

I distilled the code down to following

val spannableText = SpannableString("x\uD83D\uDE4F")
val urlSpan = URLSpan("https://www.google.com/")
spannableText.setSpan(
    urlSpan,
    1,
    2,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
) 

That produces output that looks like this:

enter image description here

In above example if I set start and end position of the span "correctly" over the entire emoji things look correct.

val spannableText = SpannableString("x\uD83D\uDE4F")
val urlSpan = URLSpan("https://www.google.com/")
spannableText.setSpan(
    urlSpan,
    1,
    3,
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)

I have code that builds complex spannable, so I got computation of span length wrong when there is codepoint/emoji in the string.

I "distilled" my issue above. Posting an update here, in case someone runs into this in the future.