Simple Alert Dialog Popup with Title, Message, Icon and Button

7 votes · 2 comments

A quick a simple Alert Dialog Popup with Title, Message, Icon and Button

Don't forget to import add the imports

import android.app.AlertDialog; import android.content.DialogInterface;

raw ·
copy
· download
AlertDialog alertDialog = new AlertDialog.Builder(this).create(); alertDialog.setTitle("Title"); alertDialog.setMessage("Message"); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO Add your code for the button here. } }); // Set the Icon for the Dialog alertDialog.setIcon(R.drawable.icon); alertDialog.show();
Add a comment

2 Comments

very useful thanks

Reply · Nov. 2, 2011, 8:22 a.m.

A good snippet. However I like chained (or what's proper name for this) calls more. Like:

new AlertDialog.Builder(this)
    .setTitle("Title")
    .setMessage("...") 
    .setIcon(...)
    .etc
Reply · July 15, 2013, 4:45 a.m.