Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Boolean from Dialog button?

Tags:

android

I have the following situation:

I have a method that performs some checks of user inputs.

the method returns true or false.

Now my method pop ups some dialogs if it is the case.

I cannot return a Boolean form onClick, because onClick returns void.

Please can anybody suggest me a workaround???

Thanks!!!

public Boolean retrunvalue=false;
private Boolean performchecks() {

    if (!reminderdatepicked && !remindertimepicked) {

        AlertDialog.Builder builder = new AlertDialog.Builder(
                NewGenericEvent.this);
        builder.setTitle(R.string.remindernotsettitle);
        builder.setMessage(R.string.remindernotmessage)
        .setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                retrunvalue= true;//NOT A GOOD IDEA
                                                      //BECAUSE IT RETURNS TRUE
                                                      //ONLY WHEN performchecks()
                                                      //IS CALLED THE SECOND TIME
            }
        });
        builder.setNegativeButton(R.string.no,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                retrunvalue= false;
            }
        });

        builder.create().show();
        return retrunvalue;
    }


    return true;
}
like image 414
Lisa Anne Avatar asked Mar 02 '26 05:03

Lisa Anne


1 Answers

What you try to do is to block main UI Thread till get response from click on OK. Not good idea.

You can find some conversation in Google groups about that issue, but solution they suggest is too complicated for your goal (your choice).

Anyways I would create Handler that should take care about button click, like:

protected static final int DIALOG_CLICKED = 1;

private Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        String txt;
        switch (msg.what) {
        case DIALOG_CLICKED:
            Boolean bool = Boolean.parseBoolean(msg.obj.toString());
            // When user press on "OK",
            // your braking point goes here
            break;
        default:
            super.handleMessage(msg);
        }
    }
};

After modify performchecks:

private void performchecks() {

        AlertDialog.Builder builder = new AlertDialog.Builder(
                this);
        builder.setTitle(R.string.remindernotsettitle);
        builder.setMessage(R.string.remindernotmessage)
        .setPositiveButton(R.string.yes,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {

                mHandler.sendMessage(mHandler.obtainMessage(DIALOG_CLICKED, true)); 
            }
        });
        builder.setNegativeButton(R.string.no,
                new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                mHandler.sendMessage(mHandler.obtainMessage(DIALOG_CLICKED, false)); 
            }
        });

        builder.create().show();           
}
like image 76
Maxim Shoustin Avatar answered Mar 04 '26 22:03

Maxim Shoustin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!