-3

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.

Suraj Makhija
  • 1,376
  • 8
  • 16
scottazord
  • 178
  • 2
  • 16

4 Answers4

1

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.

scottazord
  • 178
  • 2
  • 16
0

It lags because you remove and then readd your listener.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156
0

You seems to be trying to select the text inside the EditText. To do so, simply set editText.setSelectAllOnFocus(true); or android:selectAllOnFocus="true" in xml. Then remove the entire TextChangedListener from your code.

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
0

According to me it is most probably because of trying to remove (amountEditText.removeTextChangedListener(this);) and add (amountEditText.addTextChangedListener(this);) the Text Change Listener every time. I would recommend you to replace your code without those adding and removal.

Hope this helped.