Thị Duyên
Thị Duyên

Reputation: 61

How to check value before dismiss a dialog?

I want to show a dialog. When click outside the dialog , i want dialog to get dismiss. But before dismissing a dialog, i want to check for a condition, if correct then dismiss the dialog, else if value is incorrect don't dismiss it.

Can anyone help me? Thank you.

Upvotes: 0

Views: 898

Answers (2)

Tomaz Wang
Tomaz Wang

Reputation: 326

You can check the value when inputting with TextWatcher. Or set custom onClickListener to the positive button.

AlertDialog.Builder builder = new AlertDialog.Builder();

// build dialog

AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener(){

    @Override
    public void onShow(DialogInterface dialogInterface){


        ((AlertDialog)dialogInterface).getButton(AlertDialog.BUTTON_POSITIVE)
                                      .setOnClickListener(// check value here)


    }
}

dialog.show();

remember to invoke dialogInterface.dismiss() when you ready to dismiss it.

Upvotes: 0

T_V
T_V

Reputation: 17580

Keep setCancelabe(false); until the value in the EditText is not correct. use TextWatcherto check value, and whenever your value is correct then set setCancelabe(true);

Upvotes: 1

Related Questions