Android – Alert Dialog / Message Box / Confirmation code snippet

Context _ctx = this;
AlertDialog.Builder mBuilder = new AlertDialog.Builder(_ctx);
mBuilder.setMessage("[The message you want to show]");
mBuilder.setCancelable(false);
mBuilder.setPositiveButton("[Button caption for Yes]",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
// Do what is needed if user selects Yes
}
});
mBuilder.setNegativeButton("Button Title for No]",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
// users selected No
}
});

AlertDialog mAlertDialog = mBuilder.create();
mAlertDialog.show();

Leave a Reply