It seems to be there is no easy way to get an Alert dialog to return a simple value.
This code does not work (the answer variable cannot be set from within the listener, in fact it does not even compile)
public static boolean Confirm(Context context) {
boolean answer;
AlertDialog dialog = new AlertDialog.Builder(context).create();
dialog.setTitle("Confirmation");
dialog.setMessage("Choose Yes or No");
dialog.setCancelable(false);
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
answer = true;
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int buttonId) {
answer = false;
}
});
dialog.setIcon(android.R.drawable.ic_dialog_alert);
dialog.show();
return answer;
}
NOTE: It is important that the method is self contained, i.e., it does not depend on variables or constructs external to it. Just call it and get your answer, true or false.
So, what to do? This simple wish of returning true or false seems to be much more complicated than it deserves.
Also, the setButton method has the form:
dialog.setButton(int buttonId, String buttonText, Message msg)
But it is not clear how to use it, where is the meesage sent to, to whom, which handler is used?