Tuesday 26 July 2011

Android: how to make and show an AlertDialog

Found a fair few tutorials on how to make and show an AlertDialog but this one seems to be the easiest...

AlertDialog.Builder builder = new AlertDialog.Builder(myActivityContext);
builder.setCancelable(true);
builder.setTitle("My title");
builder.setMessage("My message");
builder.create().show();

The AlertDialog.Builder has lots of other options such as for adding buttons to the AlertDialog. See the Android Developers 'Creating Dialogs' page at the following link for more info on this and other general information about creating dialogs:
http://developer.android.com/guide/topics/ui/dialogs.html

Note: if it's just a text message you need to display to the user, consider using Toast.makeText(...).show() instead... though, however, a Toast message is only visible for a few seconds at most before it disappears so not exactly ideal if user input or confirmation is required before closing the popup.

No comments: