4

Let's say I have an edittext field and I have to implement "backspace" functionality on it.

Deleting a simple letter character is fine, it works:

Character.isLetter(inputConnection.getTextBeforeCursor(1, 0).toString()) {
   inputConnection.deleteSurroundingText(1, 0);
}

The problem comes when the character is an emoji symbol.

Its length is expressed as 2 utf-16 chars, for an example:


Grinning face:

Unicode codepoint: U+1F600

Java escape: \ud83d\ude00


In such a case, I would simply remove 2 chars.

However, there are cases where an emoji is formed by multiple codepoints, like:


Rainbow flag: ️‍

Unicode codepoint sequence: U+1F3F3 U+FE0F U+200D U+1F308

Java escape: \ud83c\udff3\ufe0f\u200d\ud83c\udf08


When I press backspace, only one java escaped char gets deleted, not whole emoji. For flag example, only this \udf08 last part would be deleted, presenting user with screwed up emoji symbol. Surrogate pair check doesn't get me out of the hole here, I would still have screwed up emoji.

How can I properly find out the correct amount of chars to remove, so I would delete 1 whole emoji when pressing backspace? (for the flag example, I would need to get the number 6, to remove it fully)

Starwave
  • 2,352
  • 2
  • 23
  • 30
  • I'm pretty sure Java has a way to check which Unicode table a particular character belongs to, then you just gotta do that for every character in the string and remove it if it's in an emoji code table. The key here being splitting on every character, not every byte. – grooveplex Feb 17 '19 at 02:24
  • Alternatively, something like https://github.com/iamcal/emoji-data will probably come in handy. – grooveplex Feb 17 '19 at 02:27

0 Answers0