I am using a custom EditText View. I have overridden the OnKeyUp event and am able to capture the Enter Key press. When the user has entered a text "Hi. How are you?" and then keeps the cursor after the word "are" and press enter, I need to get the cursor location so that i can extract the text after the cursor at the moment the Enter Key was pressed.
1 Answer
You can get the Cursor position using the getSelectionStart() and getSelectionEnd() methods. If no text is highlighted, both getSelectionStart() and getSelectionEnd() return the position of the cursor. So something like this should do the trick:
myEditText.getSelectionStart(); or
myEditText.getSelectionEnd(); Then if you want to select all the text after the cursor you could use this:
int cursorPosition = myEditText.getSelectionStart(); CharSequence enteredText = myEditText.getText().toString(); CharSequence cursorToEnd = enteredText.subSequence(cursorPosition, enteredText.length()); 1 Comment
Jo Momma
It may be helpful for some people to know, getSelectionEnd and getSelection start are static methods in the Selection class, not on EditText itself. Here are the docs: developer.android.com/reference/android/text/Selection