Android EditText listener for cursor position change

android edit text cursor position
android-edittext get cursor position
how to set cursor focus on edittext in android
android-edittext cursor position end
show cursor in edittext android programmatically
android-edittext cursor position right
android-edittext disable cursor move
android-edittext cursor not moving

I have a dialog with EditText in it. The EditText is already populated when it is created. When the user places the cursor on or near certain parts of the text a Toast will pop up.

My problem is listening for changes in cursor position. Another post asks the same question and the accepted solution was

You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as well (in this case selStart == selEnd)

onSelectionChanged (int selStart, int selEnd) is a protected method of the TextView class. How do override it?

Just subclass or extend the class EditText and add the following code to the newly create class:

 @Override 
 protected void onSelectionChanged(int selStart, int selEnd) {
        // Do ur task here.
    }

Don't forget to add constructors to the subclass. :)

Android: EditText listener for cursor position change, You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as  onSelectionChanged is available since API Level 3, i.e. Android 1.5. It is an overridable in TextView, the base class of EditText, so be sure to check the overridables of TextView in the Eclipse dialog. – Thorstenvv Jul 25 '11 at 11:59 Browse other questions tagged android cursor position android-edittext or ask your own question.

You can actually listen to selection changes without subclassing an EditText. It's a little more complicated but still manageable. To do it you need to add a SpanWatcher to a text and handle changes of selection spans.

final SpanWatcher watcher = new SpanWatcher() {
  @Override
  public void onSpanAdded(final Spannable text, final Object what,
      final int start, final int end) {
    // Nothing here.
  }

  @Override
  public void onSpanRemoved(final Spannable text, final Object what, 
      final int start, final int end) {
    // Nothing here.
  }

  @Override
  public void onSpanChanged(final Spannable text, final Object what, 
      final int ostart, final int oend, final int nstart, final int nend) {
    if (what == Selection.SELECTION_START) {
      // Selection start changed from ostart to nstart. 
    } else if (what == Selection.SELECTION_END) {
      // Selection end changed from ostart to nstart. 
    }
  }
};

editText.getText().setSpan(watcher, 0, 0, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

Android EditText listener for cursor position change?, Android EditText listener for cursor position change? final SpanWatcher watcher = new SpanWatcher() { @Override public void onSpanAdded(final Spannable text  Questions: I have a dialog with EditText in it. The EditText is already populated when it is created. When the user places the cursor on or near certain parts of the text a Toast will pop up. My problem is listening for changes in cursor position. Another post asks the same question and the accepted

I debugged a related problem across different versions of Android (feel free to comment on your findings and I'll add them to the list).

Summary of findings:


4.1.6 (Samsung device)

onSelectionChanged() gets called on TEXT EDITS only.


4.0.6 (HTC Device)

4.0.4 (reported by user Arch1tect on Samsung Note 1 device)

onSelectionChanged() gets called on cursor changes (clicks, moves etc) but NOT on Text Edits.


In the cases above where you are not informed of the section changes (text edits in some versions of Android), you will have to do that using a TextWatcher, for example in the afterTextChanged() method.

Android: EditText listener for cursor position change?, You can override onSelectionChanged (int selStart, int selEnd) to get notified about selection changes. If the cursor is moved, this is called as well (in this case​  Android :: EditText Listener For Cursor Position Change Sep 6, 2010 I'm looking for a way to detect a cursor position changed in an EditText.I couldn't find anything in the documentation so far.

Oh goodness, thanks so much for this idea. There's absolutely no reason why this feature shouldn't be in the SDK. I have an quick subclass that implements this idea but adding on the additional feature of listeners for when the selection changes. Hope it's useful

public class EditTextSelectable extends EditText {

    public interface onSelectionChangedListener {
         public void onSelectionChanged(int selStart, int selEnd);
}


private List<onSelectionChangedListener> listeners;

public EditTextSelectable(Context context) {
    super(context);
    listeners = new ArrayList<onSelectionChangedListener>();
}

public EditTextSelectable(Context context, AttributeSet attrs){
    super(context, attrs);
    listeners = new ArrayList<onSelectionChangedListener>();
}
public EditTextSelectable(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    listeners = new ArrayList<onSelectionChangedListener>();
}

public void addOnSelectionChangedListener(onSelectionChangedListener o){
    listeners.add(o);
}

protected void onSelectionChanged(int selStart, int selEnd){
    for (onSelectionChangedListener l : listeners)
         l.onSelectionChanged(selStart, selEnd);        

}

ANDROID: TextView and cursor position change event like , EditText(this.context); example: this._android = new android.inputmethodservice.​ExtractEditText(this._context); or provide a method to change  Android EditText text change listener example; RPGLE %time() cheat sheet - Current Time and Time format conversion; Java SFTP Apache commons file download, upload and delete example; Android programmatically add views - Button, TextView, EditText, RadioButton, CheckBox, ToggleButton; RPGLE convert date to numeric or character - Use %date

if anyone is still looking for a solution that does not Subclass the EditText:

(Code is in kotlin)

    editText.setAccessibilityDelegate(object : View.AccessibilityDelegate() {
        override fun sendAccessibilityEvent(host: View?, eventType: Int) {
            super.sendAccessibilityEvent(host, eventType)
            if (eventType == AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED){
                // here you can access selection of the editText 
                // with `editText.selectionStart`
                // and `editText.selectionEnd``
            }
        }
    })

android.widget.TextView, To provide user-editable text, see EditText . The following code sample shows a typical Sets the text to be displayed but retains the cursor position. final void  Android EditText listener pour le changement de position du curseur j'ai un dialogue avec EditText dedans. Le texte édité est déjà rempli lors de sa création. Lorsque l'utilisateur place le curseur sur ou près de certaines parties du texte, un Toast apparaît.

EditText, You also can receive callbacks as a user changes text by adding a TextWatcher to the edit text. This is useful when you want to add auto-save functionality as  Android :: Cursor Position In The EditText Jul 15, 2009 I was going through the notepad tutorials.the cursor was placed in the middle of edit text.i tried to make the cursor to point in the first line as shown in the pic but not able to find the solution for it.Kindly look at the pic and give favorable reply.

Android Notes 69 : How to place cursor position at the EditText?, Sometimes EditText's cursor has really stupid attitude and we need to make a static position for itself :) Starting Point : EditText editText =  Preface When using input boxes, products often have some requirements, such as 123456789 becoming 123-456-789 or restricting the input of some characters, etc. Most of the time, online search is done, but it is a waste of time to search every time, and some do not meet the needs. So write one by yourself, and …

android.widget.EditText.setTextIsSelectable java code examples , Best Java code snippets using android.widget.EditText.setTextIsSelectable (​Showing top 13 results out of 315) Change multiline EditText cursor position without opening keyboard setOnFocusChangeListener(focusListener); editText​. i have 2 edittext in my form design. what i want to acheive is that in the first edittext after typing few characters say for example i type "abc", the focus should automatically shift to the next edittext i used the following code but cant get it right

Comments
  • I've been doing this successfully in my project, but have just discovered that on some mobiles onSelectionChanged() IS called, and in others it is not being called. Both > Android 4.0 ....
  • For this application it would be better to have your toast length SHORT
  • Thankyou Guru, worked well. I have appended details of what i did to my original question if anyone is interested.
  • I get a android.view.InflateException: Binary XML file when I put this in my code... Any idea what am I doing wrong?
  • This doesn't quite work if you can't use the subclass (for instance, listening to cursor change positions in a SearchView, which is a complex layout that has an EditText embedded in it).
  • @TedHopp Please take a look at this answer: stackoverflow.com/a/36389070/170842
  • For anyone with @KamranAhmed 's problem: Did you implement the three required constructors? Does your xml element point at the right package in your project?
  • Is SpanWatcher notified only about changes inside its range? Why INCLUSIVE_EXCLUSIVE?
  • No, it's notified about all changes. It's INCLUSIVE because it shouldn't be removed when you clear the EditText.
  • Note that the watcher will be lost if you ever call setText.
  • You can avoid this by re-adding it in the afterTextChanged method of a TextWatcher.
  • I found same issue: Samsung 4.04 (Note 1) : onSelectionChanged() gets called on cursor changes (clicks, moves etc) but NOT on Text Edits.
  • hi could you help tell me how to set the OnSelectionChangedListener in the activity ?
  • @SWAppDev editText.addOnSelectionChangedListener(this) Don't forget implement your activity from EditTextSelectable.onSelectionChangedListener