Hervel
Hervel

Reputation: 35

Android Studio - How to have .addTextChangedListener work with two EditText

Currently what i'm aiming to do is to disable a button by default, then reenable when there is any values stored inside both EditTexts input1 and input2

    input1= (EditText) findViewById(R.id.input1); 
    input2= (EditText) findViewById(R.id.input2);

    go1= (Button) findViewById(R.id.Go1);

    go1.setEnabled(false); //Default turning off my button

    input1.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.toString().equals("")) {
                go1.setEnabled(false);
            } else {
                go1.setEnabled(true);
            }
        }

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

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

The above works to enable the button go1 when input1 has any values, however i'm unsure how I could work with this in order to enable the button only when BOTH input1 and input2 have a value.

Creating a second listener will just be working backwards as one would overwrite the other.

Any help would be appreciated.

Upvotes: 2

Views: 8535

Answers (2)

Siddharth Venu
Siddharth Venu

Reputation: 1388

This can be done simply using an if statement.

input1= (EditText) findViewById(R.id.input1); 
input2= (EditText) findViewById(R.id.input2);
go1= (Button) findViewById(R.id.Go1);

go1.setEnabled(false); //Default turning off my button

input1.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (s.toString().equals("")) {
            go1.setEnabled(false);
        } else {
            if(!input2.getText().toString.equals("")) //reference line **1**
               go1.setEnabled(true);
        }
    }

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

    }

    @Override
    public void afterTextChanged(Editable s) {

    }
});

And this same TextWatcher can be added to input2 as well, but you will need to edit the reference line 1 in the code from input2 to input1. This works because when input1 or input2 text's value is changed, it checks both the inputs if it is empty or not.

Upvotes: 0

betorcs
betorcs

Reputation: 2821

Verify values of both EditText's in the same time.

input1= (EditText) findViewById(R.id.input1); 
input2= (EditText) findViewById(R.id.input2);
go1= (Button) findViewById(R.id.Go1);

go1.setEnabled(false); //Default turning off my button


TextWatcher watcher = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        go1.setEnable(!TextUtils.isEmpty(input1.getText()) 
                 && !TextUtils.isEmpty(input2.getText()));
    }

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

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

input1.addTextChangedListener(watcher);
input2.addTextChangedListener(watcher);

Upvotes: 8

Related Questions