Java – How to get the response from a modal dialog

androidjava

I currently have this:

Builder yesandno = new AlertDialog.Builder(this);           
yesandno.setTitle("QuickResponse");
yesandno.setMessage(message);
yesandno.setPositiveButton("YES", null);
yesandno.setNegativeButton("NO", null);
yesandno.show();

How should I go by setting an event listener that will capture if the user clicked YES or NO?

Best Answer

When you call setPositiveButton() and setNegativeButton() instead of passing in null you should pass in a DialogInterface.OnClickListener.

For example:

yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        //User clicked yes!
    }
});