Search code examples
androidandroid-textwatcher

How to add Text Watcher on multiple Edit text in android?


I have an activity which contain two edit text and have a TextWatcher in that activity which is implemented separately. I want to make a single class which implements TextWatcher and that that TextWatcher in that edit text. How can I do that code:-

private TextWatcher getmWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty();
    }
};
private TextWatcher mWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {
        checkFieldsForEmpty();
    }
};
m_InputMobile = (EditText) findViewById(R.id.input_mobile);// finding Id of Mobile Number edit text
    m_InputMobile.addTextChangedListener(getmWatcher);
    m_InputPassword = (EditText) findViewById(R.id.input_password);// finding Id of assword editText
    m_InputPassword.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);// defining password edit Tect Input type

Solution

  • There is two ways you can do this.

    First

    TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                  if (m_InputMobile.getText().hashCode() == s.hashCode()) {
                      checkFieldsForEmpty();
                  }
                  else if (m_InputPassword.getText().hashCode() == s.hashCode()) {
                      checkFieldsForEmpty();
                  }
            }
        };
    
    m_InputMobile = (EditText) findViewById(R.id.input_mobile);
    m_InputMobile.addTextChangedListener(getmWatcher);
    m_InputPassword = (EditText) findViewById(R.id.input_password);
    m_InputPassword.addTextChangedListener(getmWatcher);
    

    or make a custome TextWatcher class

    Second

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        m_InputMobile = (EditText) findViewById(R.id.input_mobile);
        m_InputMobile.addTextChangedListener(new CustomTextWatcher(m_InputMobile));
        m_InputPassword = (EditText) findViewById(R.id.input_password);
        m_InputPassword.addTextChangedListener(new CustomTextWatcher(m_InputPassword));
    }
    
    private class CustomTextWatcher implements TextWatcher {
        private EditText mEditText;
    
        public CustomTextWatcher(EditText e) { 
            mEditText = e;
        }
    
       @Override
       public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
       }
    
       @Override
       public void onTextChanged(CharSequence s, int start, int before, int count) {
    
       }
    
       @Override
       public void afterTextChanged(Editable s) {
           checkFieldsForEmpty();
       }
    }
    

    For more details visit this Question.

    Happy Coding.