15

On my application I put TextWatcher on EditText. When I change the text of the EditText, the events of TextWatcher are being called twice.

I am using emulator for running the app.

Dimitar
  • 4,402
  • 4
  • 31
  • 47
Jay Gajjar
  • 2,661
  • 2
  • 21
  • 35
  • see here http://stackoverflow.com/questions/17535415/textwatcher-events-are-being-fired-multiple-times – Hun Apr 06 '15 at 08:10

3 Answers3

10

How does your code looks like? that is the normal behaviour of TextWatcher. Example:

myInput.addTextChangedListener(new TextWatcher() {
        boolean mToggle = false;

        public void onTextChanged(CharSequence cs, int s, int b, int c) {}

        public void afterTextChanged(Editable editable) {
            if (mToggle) { 
                Toast.makeText(getBaseContext(), "HIT KEY",Toast.LENGTH_LONG).show();
            }
            mToggle = !mToggle;
        }

        public void beforeTextChanged(CharSequence cs, int i, int j, int k) {}
    });
TheoKanning
  • 1,631
  • 11
  • 20
nano_nano
  • 12,351
  • 8
  • 55
  • 83
3

My problem was I added the textWatcher twice mEditText.addTextChangedListener(mTextWatcher), which leads to calling its callbacks twice!

I had added the textWatcher once in onCreate() and once in onStart(). I should only add in onStart and remove in onStop().

mco
  • 1,809
  • 15
  • 31
0

In case you call editText.setText("string") inside your TextWatcher listener (for example in afterTextChanged method), TextWatcher will detect a new text change, that might end up by calling again setText and create a loop.

An alternative to editText.setText("string") could be:

editText.getText().clear();
editText.append("string");

which will not evoke a new detection by the TextWatcher listener.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
Amir Golan
  • 378
  • 3
  • 8