I have an alertDialogBuilder
of type AlertDialog.Builder
. This has two buttons, one positive and another one negative. When the positive button is clicked, I have a condition check and if it is successful, only then the alertDialogBuilder
should be closed else the android app should keep displaying it. Is this possible?
Current code
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("test");
alertDialogBuilder.setMessage("testMessage");
alertDialogBuilder.setCancelable(false);
editText = new EditText(this);
editText.setText("hi");
alertDialogBuilder.setView(editText);
editText.requestFocus();
alertDialogBuilder.setNegativeButton("Cancel", dialogLinstener);
alertDialogBuilder.setPositiveButton("Save", dialogLinstener);
alertDialogBuilder.show();
}
private DialogInterface.OnClickListener dialogLinstener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(which == DialogInterface.BUTTON_POSITIVE) {
String str = editText.getText().toString();
if(!str.equals("hi")) {
// do something..
} else {
// do something else..
}
} else if (which == DialogInterface.BUTTON_NEGATIVE) {
//do nothing.
}
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null)
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
return;
}
};