views:

65

answers:

1

I am trying to add a yes/no confirmation popup to an android OnClickListener. Is it OK to use an AlertDialog.Builder in a setOnClickListener or should I be taking a different approach? I have stepped through the following code using the eclipse/android debug environment and expect the popup to appear at the .create, awaiting user response, but it does not. I am new to android and java so I may be missing something obvious. Any advice, ideas, or direction would be appreciated.

public class Controller extends Activity {
...
        buttonOn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AlertDialog.Builder(Controller.this)
                    .setIcon(R.drawable.ic_menu_help)
                    .setMessage("Are You Sure?")
                    .setPositiveButton("OK", 
                            new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, 
                                int whichButton) {
                        // Positive response code
                    }
                })
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        // Negative response code
                    }
                })
                .create();
            }
        });
+4  A: 

Use AlertDialog.Builder#show instead of create. create returns an AlertDialog object but doesn't show it.

Qberticus
I can't Up Vote since I don't have 15 reputation but thanks a million. That was exactly what I needed.
JackN
Yup .show(); does it :)
stealthcopter