Android: Display alert dialogs

It is useful to display confirmation dialogs in your app to prevent accidental presses.

There is an inbuilt feature to do that, and its called the AlertDialog Builder.

This may look a bit messy, but its all chained together. You can split it up into separate calls if you want to make it look a bit neater.

AlertDialog.Builder ad = new AlertDialog.Builder(activity);
ad.setIcon(android.R.drawable.ic_dialog_alert) .setTitle("Quit game?")
.setMessage("Are you sure you want to leave this game?")
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// User said no, resume game?
}
})
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Stop the activity
}
});

ad.show();
 
Copyright © Twig's Tech Tips
Theme by BloggerThemes & TopWPThemes Sponsored by iBlogtoBlog