1

Hello I would like to do this : I have two EditText and one button and I would like to disable the button if one of the EditText is empty. To do this I try this :

if(editText1!!.getText().length  == 0 || editText2!!.getText().length  == 0 ){
            button1!!.setEnabled(false)
        }

The problem is my button is everytime disable... and I would when the two EditText are not empty I can click on the button...

How can I do this ?

Thank you !

K.Os
  • 5,123
  • 8
  • 40
  • 95

1 Answers1

1

Edited : after clarification from OP. If you want to check two editTexts , you can do something like following,

        boolean editText1Empty = true;
        boolean editText2Empty = true;

        editText1.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) {
                editText1Empty = charSequence.length() == 0;
                checkButton();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        editText2.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) {
                editText2Empty = charSequence.length() == 0;
                checkButton();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        void checkButton() {
        button.setEnable(!editText1Empty && !editText2Empty);
        }
Dexter
  • 1,421
  • 3
  • 22
  • 43