4

I'm trying to bring up an alert dialog box when one of my OptionsMenuItems is clicked. Proceed with operation if user clicks "yes", cancels if clicks no. I just don't know how to do the code. This is what I have:

@Override
public boolean onOptionsItemSelected(MenuItem item)
{ 

    switch (item.getItemId()) { 
    case R.id.exit:
        this.finish();
        return true;
    case R.id.about:
        Intent i = new Intent(this, AboutActivity.class);
        this.startActivity(i);
    case R.id.skip:
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

            // set the message to display
            alertbox.setMessage("The yoga pose will be skipped from now on!").show();
           alertbox.setCancelable(true);
           alertbox.setNegativeButton("no").setPositiveButton("OK", new DialogClicklistener() { 

             // click listener on the alert box
            public boolean onClick(DialogInterface arg0, int arg1) {
                // the button was clicked
                boolean success = myDbHelper.setSkip(PoseID);
                SetImageView2(myDbHelper);
                return success;
              }

           });

           // add a neutral button to the alert box and assign a click listener
           alertbox.setCancelable(true).set(onCancelListener) {

              // click listener on the alert box
               public boolean onClick(DialogInterface arg0, int arg1) {
                // the button was clicked

              }
           });
           // show it
           alertbox.show();

     default: 
            return super.onOptionsItemSelected(item);
    }
}
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • And what is wrong with the current code ? – sdabet Oct 30 '12 at 16:02
  • It won't compile. I don't have the alert code correct to get the onClick behavior set properly. – Kristy Welsh Oct 30 '12 at 16:05
  • What is the problem? Seems like what you have there should call SetImageView2 when the user clicks 'OK'. You actually do not need the onCancelListener it seems so I would nuke that part of the code. You appear to not be creating the dialog though.. – ian.shaun.thomas Oct 30 '12 at 16:05
  • Yes, I don't know how to write the code for the alert dialog buttons. This is not correct code. Eclipse is showing all kinds of errors, I am just stuck. – Kristy Welsh Oct 30 '12 at 16:10
  • Also, there should be an option for the user to say, "Yes, cancel out of this". I think we need it. – Kristy Welsh Oct 30 '12 at 16:11

2 Answers2

6

The following is directly from the app I'm currently working on. It brings up an AlertDialog which then takes the user to a different activity (after they enter a password). Please let me know if I can clarify anything about it.

Edit: whole method now included.

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    final int menuItem = item.getItemId();
    if ((menuItem == R.id.survey) || (menuItem == R.id.syncMode)) {
        View layout = inflater.inflate(R.layout.survey_password_dialog, (ViewGroup) findViewById(R.id.layout_root));
        final EditText password = (EditText) layout.findViewById(R.id.passwordText);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);            
        builder.setView(layout)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {   
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        });
        final AlertDialog alertDialog = builder.create();
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                b.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (password.getText().toString().equalsIgnoreCase("the_password")) {
                            if (menuItem == R.id.survey) {
                                Intent intent = new Intent(AsthmaAppActivity.this, SurveyActivity.class);
                                startActivity(intent);
                                alertDialog.dismiss();
                            }
                            else { //(menuItem == R.id.syncMode) 
                                startActivity(new Intent(AsthmaAppActivity.this, SyncMode.class));
                                alertDialog.dismiss();
                            }
                        }
                        else Toast.makeText(AsthmaAppActivity.this, "Password incorrect", Toast.LENGTH_LONG).show();
                    }
                });
            }
        });
        alertDialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        alertDialog.show();
    }
    else {  //dialog for setting application parameters "on the fly" for application testing
        View layout = inflater.inflate(R.layout.parameter_change, (ViewGroup) findViewById(R.id.layout_root));
        final EditText parameter = (EditText) layout.findViewById(R.id.parameterText);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout)
        .setCancelable(false)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {  
                String parameterString = parameter.getText().toString();
                if(parameterString == null || parameterString.isEmpty()) {
                    testParam = 0.0;
                } 
                else {
                    testParam = Double.parseDouble(parameterString);
                }
            }
        })
        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                dialog.cancel();
            }
        })
        .show();
    }
    return(super.onOptionsItemSelected(item));
} 
hBrent
  • 1,696
  • 1
  • 17
  • 38
  • This seems to be what I need. And Yes, I would love to see the rest of the code. – Kristy Welsh Oct 30 '12 at 16:13
  • Glad to help. See the edited answer with the whole method (2 different AlertDialogs used, for different menu items). – hBrent Oct 30 '12 at 16:19
  • One question, why do you .setCancelable(false) instead of true? – Kristy Welsh Oct 30 '12 at 16:26
  • Honestly, I'm not sure, though I think in my case it's making sure that the user can't bypass the dialog box and still go to the activity without entering the password. The dialog box is cancelable via the cancel button, but that just takes the person to the activity they started from, not the different one they go to if they enter the password correctly and hit OK. Note, too, that if the person enters an incorrect password, a toast tells them so, but the dialog doesn't disappear, so they have a chance to enter the correct password. .setCancelable(false) may be taking care of that. – hBrent Oct 30 '12 at 16:34
  • The behavior seems the same whether or not I set it to true or false. – Kristy Welsh Oct 30 '12 at 16:43
  • Hmmm... maybe it's not doing anything :). I haven't tried to change it in my app to see the effect, but I can if you need to know. Let me know if you're not getting some behavior that you need. – hBrent Oct 30 '12 at 16:58
0
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
alertbox.setPositiveButton("Yes" new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    log("i'm clicked");
                }
            });
Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158