0

I need a listener which is called every time the selection in an EditText changes. I googled around but I couldn't find anything useful for API level 7. I'm writing a Text Editor and I want the bold/italic/underlined button appear selected every time the user selects bold/italic/underlined text.

Sazbak
  • 189
  • 1
  • 14
  • Have you tried this? http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29 – Shubhayu Apr 17 '12 at 09:19
  • According to the description of TextWatcher "Adds a TextWatcher to the list of those whose methods are called whenever this TextView's text changes. " What I need is a listener that informs when the selection on the text is changed not the text. PS: I've just tried it by the way and none of the TextWatcher's listener was called when the selection changed. – Sazbak Apr 17 '12 at 10:32
  • Are you extending an EditText to make your editor? – Shubhayu Apr 17 '12 at 10:42
  • Not yet. I was hoping for such listener to exist. I guess I should override the setSelection method of the EditText or something like that. – Sazbak Apr 17 '12 at 10:49
  • There is a onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) method which you could override in that case. Will check if i find any more info about a text changed listener. – Shubhayu Apr 17 '12 at 10:53
  • I didn't find any such listener but after looking into stuff, I think you need to play around with the Touch Listeners. Also EditText has a method called didTouchFocusSelect() http://developer.android.com/reference/android/widget/TextView.html#didTouchFocusSelect%28%29 which may be of interest to you. – Shubhayu Apr 17 '12 at 11:00
  • Didn't you mean onSelectionChanged!? Because I've just found it among the methods of TextView. 1 sec I'll test it out. – Sazbak Apr 17 '12 at 11:04
  • Either would work, depending upon how you want to go about implementing it :) – Shubhayu Apr 17 '12 at 11:25
  • Finally it works....i completely missed this method somehow. Thanks for taking your time to help me! – Sazbak Apr 17 '12 at 11:31
  • You are welcome :) Do you want me to add it as an answer to the question? – Shubhayu Apr 17 '12 at 11:40
  • Yes please. --------------------- – Sazbak Apr 17 '12 at 11:51

2 Answers2

4

Pretty old question, but someone might still need this, so here's my solution : since the text selection accomplished with long press on the text, I simply used the following :

editText.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View view) {
        // do whatever you need to do on text selection
    }
});

This allows for custom behavior on text selection and doesn't prevent the user from copying/pasting.

2Dee
  • 8,609
  • 7
  • 42
  • 53
  • 3
    Be sure to return false here if you still want the EditText to perform its longPress handler and select the text, if you return true all you'll get is your onLongClick() exectuted. – Michael Marsella Apr 13 '16 at 17:08
0

The better way to do it would be to extend the EditText and then based upon how you would want to manage the changing text, you could override one of the 2 methods to work out your customized behavior.

  1. If you want the selection changed then you could use the onSelectionChanged() method and implement your code there.

  2. In case you want to implement something when the text changes in your editor then you could use, onTextChanged().

Shubhayu
  • 13,402
  • 5
  • 33
  • 30