How to display an AlertDialog in your Android application

SDK Version: 

Here is an example of an applacation, that illustrates how you can create a simple AlertDialog. The dialog can display maximum three buttons.

First of all declarate the AlertDialog type object:

  1. AlertDialog alertDialog = new AlertDialog.Builder(Main.this).create();

Main.this is my activity's context.

You can set your dialog's title like this:

  1. alertDialog.setTitle("Reset...");

And a message:

  1. alertDialog.setMessage("R u sure?");

Next thing, set your button(s) functions:

  1.   alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
  2.       public void onClick(DialogInterface dialog, int which) {
  4.        //here you can add functions
  6.     } });

And you can change the icon of your AlertDialog using this line:

  1. alertDialog.setIcon(R.drawable.icon);

Last thing, don't forget to show your dialog: