Qandil Tariq
Qandil Tariq

Reputation: 539

onTextChanged is not working properly in custom EditText

I need to customised an edit text, when I enter text I will format it on text changed. I have implemented the text watcher in edit text, below is the code:

public class CustomEditText extends androidx.appcompat.widget.AppCompatEditText {

public CustomEditText(Context context) {
        super(context);
        init();
    }

    public CustomEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
  TextWatcher textChangeListener = new TextWatcher() {
        boolean isIgnoreChange = false;
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int position, int noToAdd, int 
        noToDelete) {

            CustomEditText.this.removeTextChangedListener(textChangeListener);
            CustomEditText.this.setText("personal"); // with some logic

             CustomEditText.this.addTextChangedListener(textChangeListener);                

        }

        @Override
        public void afterTextChanged(Editable editable) {
        }

    };
private void init() {
    this.addTextChangedListener(textChangeListener);
}


}

And I've wrapped my EditText in TextInputLayout in layout.

Issue: When I removeTextChangedListener and called the setText method, still onTextChanged called one more time. I've attached the android source code of onTextChanged and debugged the code, I have got to know that TextInputLayout is also attached in listeners list, which I have not added and maybe that is calling. Multiple calling after remove the listener is disturbing my logic. If someone gets any hint, which I am missing Please help.

Upvotes: 1

Views: 1360

Answers (2)

Gioan Le
Gioan Le

Reputation: 1168

In the init() method, you have to add the listener to the CustomEditText:

public void init() {
    this.addTextChangedListener(textChangeListener);
}

AND you should remove the below code in the onTextChanged() method

CustomEditText.this.removeTextChangedListener(textChangeListener);
CustomEditText.this.addTextChangedListener(textChangeListener);

I recommend you to have a look on this tutorial at 19:00s

Upvotes: 0

Twisha Kotecha
Twisha Kotecha

Reputation: 1104

Add click listener of EditText like following and do according your requirement into all of these methods :-

onTextChanged,beforeTextChanged,afterTextChanged add actions what you want into these methods.

et.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {

    // TODO Auto-generated method stub
 }

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    // TODO Auto-generated method stub
 }

@Override
public void afterTextChanged(Editable s) {

    // TODO Auto-generated method stub
 }
});

Hope this will help you.

Upvotes: 1

Related Questions