Yes or No Dialog

Most important we will learn YesNoDialog or we can say ConfirmDialog. This dialog often use in your project when you need to know only confirmation to do something.

Let's see following example of ConfirmationDialog as:

Example:
public void confirmDialog(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Confirmation");
    builder.setMessage("Do you want to continue?");

    builder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
            Toast.makeText(MainActivity.this, "You Confirm to proceed.", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            dialogInterface.dismiss();
            Toast.makeText(MainActivity.this, "You rejected.", Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

    // below line keep dialog displayed when rotate screen, iff these lines not present
    // an exception will through, so keep best practice to include these lines
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
    dialog.getWindow().setAttributes(lp);
}