Android keyboard InputConnection setSelection doesn't work well with unicode?

Viewed 58

When pasting a text in the editor with unicode in it, for example:

 Please give me an authentic answer buddy.

And doing:

getCurrentInputConnection().setSelection(31, 43);

The selected text is not at the right place. Probably because of the two special unicode characters at the beginning?

1 Answers

I believe this function fixes the issue:

public void setSelectionByUnicodePosition(InputConnection inputConnection, String currentString, int start, int end){
        int firstUnicodeposition  = currentString.offsetByCodePoints(0, start);
        int secondUnicodePosition = currentString.offsetByCodePoints(0, end);
        inputConnection.setSelection(firstUnicodeposition, secondUnicodePosition);
    }
Related