Skip to content Skip to sidebar Skip to footer

Alertdialog Return Boolean Value

I am trying to include an AlertDialog builder within a method that prompts for a pin code and when the positive button is pressed, checks it against a database value and returns a

Solution 1:

A dialog can't "return" a value in the way that it looks like you're expecting. A dialog can make changes to some other object, but you can't have a bit of code block on it and wait for the user to finish interacting with it.

Instead, you'll need to set up listeners for when the prompt dialog is dismissed or buttons or clicked, or whatever other event signals that you have what you need from it. Those listeners can then read the data gathered and set by the dialog.

Solution 2:

You can create your own method to generate dialog with listener:

publicvoidisCorrectPin(Context context, String title, String message, String btnPositive, final DialogSingleButtonListener dialogSingleButtonListener) {
    final AlertDialog.BuilderdialogBuilder=newAlertDialog.Builder(context);
    dialogBuilder.setTitle(title);
    dialogBuilder.setMessage(message);
    dialogBuilder.setPositiveButton(btnPositive, newDialogInterface.OnClickListener() {
        @OverridepublicvoidonClick(DialogInterface dialog, int which) {
            if (editText.getText().toString() == getPinCode()){
                dialogSingleButtonListener.onButtonClicked(dialog);
            }
        }
    });

    AlertDialogdialog= dialogBuilder.create();
    dialog.show();
}

And the listener class:

publicinterfaceDialogSingleButtonListener {   
    publicabstractvoidonButtonClicked(DialogInterface dialog); 
}

And use it like:

service.isCorrectPin(context, title, message, btnPositive
       newDialogSingleButtonListener() {
                @Override
                publicvoidonButtonClicked(DialogInterface dialog) {
                     //code here is only called if they entered a correct pin.
                }
       }

);

Solution 3:

this is how i'm doing :

publicBooleanshowAlert(String message)
{
    action = false;

    AlertDialog.Builder alertDialog = newAlertDialog.Builder(HAActivity.this);

    // Setting Dialog Title
    alertDialog.setTitle(getString(R.string.app_name));

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting Icon to Dialog// Setting Positive "Yes" Button
    alertDialog.setPositiveButton("OK", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog,int which) {

            // Write your code here to invoke YES event
            action = true;
        }
    });

    // Setting Negative "NO" Button
    alertDialog.setNegativeButton("Cancle", newDialogInterface.OnClickListener() {
        publicvoidonClick(DialogInterface dialog, int which) {
            // Write your code here to invoke NO event
            action = false;
            dialog.cancel();
        }
    });

    // Showing Alert Message
    alertDialog.show();

    return action;
}

and calling function like this : //activity in which you create function

if (Activity.showAlert("Do you really want to delete ??"))
            {
               //delete it anyway.

            }

Post a Comment for "Alertdialog Return Boolean Value"