Get cursor position in Android in text editor?

I am using a custom view of EditText. I redefined the OnKeyUp event and was able to capture the keystroke of the enter key. Now my requirement is when the user entered the text "Hello. How are you?" and then holds the cursor after the word "is" and presses enter, I need to get the location of the cursor so that I can extract the text after the cursor at the moment of pressing the enter key. Please let me know how to do this. Thanks for your time and help.

+44
android android-keypad android-edittext
Aug 01 2018-11-11T00:
source share
1 answer

You can get the cursor position using the getSelectionStart () and getSelectionEnd () methods. If no text is selected, both the getSelectionStart () and getSelectionEnd () methods return the cursor position. 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 can use this:

 int cursorPosition = myEditText.getSelectionStart(); CharSequence enteredText = myEditText.getText().toString(); CharSequence cursorToEnd = enteredText.subSequence(cursorPosition, enteredText.length()); 
+105
Aug 01 2018-11-11T00:
source share



All Articles