Events of TextWatcher are being called twice

Jay Gajjar picture Jay Gajjar · Aug 22, 2012 · Viewed 8.4k times · Source

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.

Answer

s_bei picture s_bei · Aug 22, 2012

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) {}
    });