0

I have a Sign Up screen with bunch of EditTexts and each one has a TextWatcher so I can "listen" for changes and show or hide errors.

For example :

 etFirstName.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if (etFirstName.getText().length() > 0) {
                hideFirstNameError();
            }
        }
    });

The problem is that the code is too bloated with all this code being repeated again and again for each one of my editTexts.

So I was just wondering if there is a cleaner way to write it ? Tried to replace with a custom class that extends TextWatcher but then I need to pass as variables to the constructor the EditTexts and I'm not so sure if this is a good practice : passing to other classes than your Activities components that have to do with android ?

Mes
  • 1,671
  • 3
  • 20
  • 36

1 Answers1

0

Create a custom class:

public class CustomTextWatcher implements TextWatcher {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        //empty
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        //empty
    }

    @Override
    public void afterTextChanged(Editable s) {
        //empty
    }
}

And pass it instead of new TextWatcher().

Roman Samoilenko
  • 932
  • 12
  • 25