Get cursor position for Android

I am working on a soft Android keyboard and wondering if there is a way for the keyboard to get the current cursor position? I am currently using the following code:

connection.getTextBeforeCursor(Integer.MAX_VALUE, 0).length()

However, it is very slow (even for a small amount of text it can take up to 50 ms - it works on the Galaxy Nexus, so it will probably be even slower for phones with a lower number). I also tested it on the Droid Incredible, and the lag is even more serious.

The onUpdateSelection function gives you a new cursor position. However, this function is not always called and, therefore, storing the value that it provides for future use is not reliable.

Since you can set the cursor position and get the selected text (but not the position of the selected text), shouldn't there be a function to get the cursor position?

Thanks for the help!

+5
source share
1 answer

This is an old question, but I have encountered the same problem recently. To get the cursor position:

InputConnection ic = getCurrentInputConnection();
ExtractedText et = ic.getExtractedText(new ExtractedTextRequest(), 0);
int selectionStart = et.selectionStart;
int selectionEnd = et.selectionEnd;
+4
source

All Articles