I have some code that utilizes the Material Dialogs library; I have a dialog with four EditText fields (email, name, username, password). I want it so that the dialog's Register button is disabled unless all fields have at least one character in them.
I searched around, and found a way to do this; unfortunately, it doesn't appear to work. For example, if I pull up the dialog and type anything into any single field, the button becomes enabled. However, if I edit a single field and then edit another field but then delete the text inside one of those fields--the button becomes disabled; the same thing happens if I were to fill out all of the fields, then delete the text inside a single field.
I had thought of utilizing another of the suggestions (which would involve writing my own private inner class)--but I didn't think that would matter considering I'd achieve the same thing (at least, to my knowledge).
//registerDialog is a MaterialDialog object
final View registerAction = registerDialog.getActionButton(DialogAction.POSITIVE);
final EditText registerNameInput;
final EditText registerEmailInput;
final EditText registerUsernameInput;
final EditText registerPasswordInput;
if (registerDialog.getCustomView() != null) {
registerNameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_name);
registerEmailInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_email);
registerUsernameInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_username);
registerPasswordInput = (EditText) registerDialog.getCustomView().findViewById(R.id.register_password);
/*
* TextWatcher lets us monitor the input fields while registering;
* This make sure we don't allow the user to register with empty fields
*/
TextWatcher watcher = 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) {
registerAction.setEnabled(s.toString().trim().length() > 0);
}
@Override
public void afterTextChanged(Editable s) {
}
};
/*We want to watch all EditText fields for input*/
registerNameInput.addTextChangedListener(watcher);
registerEmailInput.addTextChangedListener(watcher);
registerUsernameInput.addTextChangedListener(watcher);
registerPasswordInput.addTextChangedListener(watcher);
}
registerDialog.show();
registerAction.setEnabled(false); //disabled by default