Android: Two Option (Yes/No) Dialog Pop Up

Mobile applications often need to have a two option or yes/no decision dialog. A common case is to delete posts and entries, or requesting if a user really want to do a certain action. I wrote a short and simple code snippet which can be inserted everywhere into the app project.

DialogInterface.OnClickListener dialogClickListener = 
new DialogInterface.OnClickListener() {

     public void onClick(DialogInterface paraDialog, int paramButton) {
          switch (paramButton) {
               case DialogInterface.BUTTON_POSITIVE:
                    // Put the code here which should be executed on left button click
                    break;

               case DialogInterface.BUTTON_NEGATIVE:
                    //Put the code here which should be executed on right button click
                    break;

          }
     }
};


AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("What do you want to do?")
.setPositiveButton("Delete Post", dialogClickListener)
.setNegativeButton("Archive Post", dialogClickListener).show();

A screen shot how the result looks like:

Leave a comment