Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events of TextWatcher are being called twice

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.

like image 540
Jay Gajjar Avatar asked Aug 22 '12 13:08

Jay Gajjar


2 Answers

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) {}
    });
like image 166
s_bei Avatar answered Sep 19 '22 05:09

s_bei


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().

like image 43
mco Avatar answered Sep 20 '22 05:09

mco