peter_budo peter_budo - 1 month ago 16
Android Question

Do not close DialogPreference onClick if condition not met

I have need for AutoCompliteTextView in PreferenceActivity so I extended DialogPreference. My auto-complite is expecting from (helping) user to enter country name. I'm fine if user press Cancel or enters no values, however I would like to ensure that proper name is entered before dialog is closed. I tried to override onClick as


@Override
public void onClick(DialogInterface dialog, int which) {
if (!validator.isValid(textView.toString())) {
onDialogClosed(false);
} else {
//do something here
super.onClick(dialog, which);
}
}


also with onDialogClosed


@Override
protected void onDialogClosed(boolean positiveResult) {
if (validator.isValid(textView.toString())) {
//do something here
super.onDialogClosed(positiveResult);
}
}

Answer

Once a user clicks on a dialog button, the dialog is closing, and there is nothing you can do to stop it.

The only thing I can think of that you could try is to call getDialog() on the DialogPreference, cast that to AlertDialog, then call getButton() to retrieve your positive button, and disable it, enabling it later when the input is valid.