1

I've implemented a softkeyboard for android with a built in emoji keyboard.

my problem is with the backspace button , when i delete a char it's ok but when i delete an emoji it doesn't do it well. code:

  final EmojiconsPopup popup = new EmojiconsPopup(kv.getRootView(), this);
    popup.setSizeForSoftKeyboard();
    popup.setOnEmojiconBackspaceClickedListener(new EmojiconsPopup.OnEmojiconBackspaceClickedListener() {
        @Override
        public void onEmojiconBackspaceClicked(View v) {
            InputConnection ic = getCurrentInputConnection();
            ic.deleteSurroundingText(1, 0);
        }
    });

If i change the line to

ic.deleteSurroundingText(2, 0);

The emojis are deleted well , but the chars will be deleted two at a time .

Any ideas on how to solve this issue ?

Update

after a little more investigating , i found out some emojis are one char (like a regular letter) and some two chars , Any ideas on how to differ between them ?

yanivtwin
  • 617
  • 8
  • 32
  • how about checking the character/emoji being deleted and using `ic.deleteSurroundingText()` accordingly? – Frogger Jan 11 '17 at 11:38
  • that's what i'm trying to do , i managed to get the last char , but how will i know if it's an emoji or not ? – yanivtwin Jan 11 '17 at 12:23
  • 1
    You can try out something using this [answer](http://stackoverflow.com/a/30767690/4630403), You can split the last two characters of the string and check if it matches the regex, then delete it accordingly. – Frogger Jan 13 '17 at 04:23
  • 1
    managed to solve it with http://stackoverflow.com/q/41609277/7079340 – yanivtwin Jan 14 '17 at 14:10

1 Answers1

0

Try this, check if its an emoji using Charactor.isSurrogate

CharSequence c = inputConnection.getTextBeforeCursor(1, 0);
if(c.length() > 0 && Character.isSurrogate(c.charAt(0))){
        KeyEvent keyEventDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL);
        inputConnection.sendKeyEvent(keyEventDown);
        return;
}
chathura
  • 3,362
  • 6
  • 41
  • 68
  • This doesn't work for all of the existing emojis, only for the basic ones. For other emojis it can still take 2-4 backspace pressed to delete them – Idan Jan 08 '21 at 07:46