Search code examples
androidandroid-layoutandroid-edittextandroid-textwatcher

Lag after setting text in OnTextChanged()


Below is an example that replicates my lagging problem. Once I set the text on the EditTextView it takes at least 1.5 seconds for the user to be allowed to input another character.

    amountEditText.addTextChangedListener(new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        @Override public void afterTextChanged(Editable s) {}
        @Override public void onTextChanged(CharSequence s, int start, int before, int count) {
            String amount = s.toString();

            if( !amount.equals(current )) {
                amountEditText.removeTextChangedListener(this);
                amountEditText.setText(s);
                Selection.setSelection(amountEditText.getText(), amountEditText.getText().length());
                amountEditText.addTextChangedListener(this);
            }
        }
    });

I've searched around and have not found a solution.


Solution

  • I identified that the issue was coming from the textView.setText() call.

    The solution was to not use setText(), instead use the Editable that is provided to you in the onTextChanged callback.

    I tried to use the Editable before, however i couldn't get it working with inputs such as "$12,000".

    This was due to having InputFilters still attached to the Editable.

    Regards, Scott.