Home > OS >  Enable button when edit text and checkbox are are fill and vice versa
Enable button when edit text and checkbox are are fill and vice versa

Time:01-16

Basically, I want to enable the button when both edit text and checkbox are fill if any one of not fill then disable the button in android with java

CodePudding user response:

you can add the condition inside setOnClickListener function i.e

button.setOnClickListener {
 if(editText.text.toString().isEmpty() || !checkBox.isChecked) return
 // do your job here...
}

CodePudding user response:

you can try do it like following:

check for editText if its empty and if the checkbox is checked, so if editText isn't empty and checkbox is checked that will mean button shall be enabled,else disabled and apply same condition for on checkbox listener

condition will look as follows

yourButton.setEnabled(yourCheckBox.isChecked() && !editText.text.toString().isEmpty())

example on text watcher implementation:

editText.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) {
  yourButton.setEnabled(yourCheckBox.isChecked() && !editText.text.toString().isEmpty())
}
});

the reason you will need this on checkbox and on textwatcher at same time, is cause textwatcher can tell when editText is changed in realtime, on other hand the checkbox as far as I know it ain't possible thats why you will need the other condition set on checkbox click listener as well.

  •  Tags:  
  • Related