Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button(ok button), two buttons(yes or no buttons) and three buttons(yes, no and cancel buttons).

 

Android alert dialog with One button

The following code will create a simple Alert Dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.

AlertDialog alertDialog = new AlertDialog.Builder(
        MainActivity.this).create();

// Setting Dialog Title
alertDialog.setTitle("Alert Dialog");

// Setting Dialog Message
alertDialog.setMessage("Welcome to CodeDecode.in");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);

// Setting OK Button
alertDialog.setButton("OK",
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                                int which) {
                // Write your code here to execute after dialog
                // closed
                Toast.makeText(getApplicationContext(),
                        "You clicked on OK", Toast.LENGTH_SHORT)
                        .show();
            }
        });

// Showing Alert Message
alertDialog.show();

This output of about code will be like following image.

Alert Dialog With One Button

Android alert dialog with two button

The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");

// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);

// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
            }
        });
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog
                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                dialog.cancel();
            }
        });

// Showing Alert Message
alertDialog.show();

This output of about code will be like following image.

alert dialog with two button

Android alert dialog with three button

Here setNeutralButton() is used to create a neutral cancel button.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(
        MainActivity.this);

// Setting Dialog Title
alertDialog.setTitle("Save File...");

// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");

// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);

// Setting Positive Yes Button
alertDialog.setPositiveButton("YES",
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                                int which) {
                // User pressed Cancel button. Write Logic Here
                Toast.makeText(getApplicationContext(),
                        "You clicked on YES",
                        Toast.LENGTH_SHORT).show();
            }
        });
// Setting Positive Yes Button
alertDialog.setNeutralButton("NO",
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                                int which) {
                // User pressed No button. Write Logic Here
                Toast.makeText(getApplicationContext(),
                        "You clicked on NO", Toast.LENGTH_SHORT)
                        .show();
            }
        });
// Setting Positive "Cancel" Button
alertDialog.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog,
                                int which) {
                // User pressed Cancel button. Write Logic Here
                Toast.makeText(getApplicationContext(),
                        "You clicked on Cancel",
                        Toast.LENGTH_SHORT).show();
            }
        });
// Showing Alert Message
alertDialog.show();

This output of about code will be like following image.

alert dialog with two button

Final Code 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="#c0c0c0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <Button
        android:id="@+id/btnAlert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dip"
        android:background="#F06292"
        android:padding="10dip"
        android:text="Show Alert"
        android:textColor="#fff"
        android:textStyle="bold"></Button>

    <Button
        android:id="@+id/btnAlertWithTwoBtns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Alert With Two Buttons"
        android:padding="10dip"
        android:layout_marginTop="10dp"
        android:background="#F06292"
        android:textColor="#fff"
        android:textStyle="bold">
    </Button>

    <Button
        android:id="@+id/btnAlertWithThreeBtns"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Show Alert with Three Buttons"
        android:padding="10dip"
        android:layout_marginTop="10dp"
        android:background="#F06292"
        android:textColor="#fff"
        android:textStyle="bold">
    </Button>
</LinearLayout>

MainActivity.java

package com.codedecode.yashpwr;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btnAlert = findViewById(R.id.btnAlert);
        Button btnAlertTwoBtns = findViewById(R.id.btnAlertWithTwoBtns);
        Button btnAlertThreeBtns = findViewById(R.id.btnAlertWithThreeBtns);

        btnAlert.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Creating alert Dialog with one Button
                AlertDialog alertDialog = new AlertDialog.Builder(
                        MainActivity.this).create();

                // Setting Dialog Title
                alertDialog.setTitle("Alert Dialog");

                // Setting Dialog Message
                alertDialog.setMessage("Welcome to CodeDecode.in");

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.tick);

                // Setting OK Button
                alertDialog.setButton("OK",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // Write your code here to execute after dialog
                                // closed
                                Toast.makeText(getApplicationContext(),
                                        "You clicked on OK", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();
            }
        });

        /**
         * Showing Alert Dialog with Two Buttons one Positive Button with Label
         * "YES" one Negative Button with Label "NO"
         */
        btnAlertTwoBtns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Creating alert Dialog with two Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Confirm Delete...");

                // Setting Dialog Message
                alertDialog.setMessage("Are you sure you want delete this?");

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.delete);

                // Setting Positive "Yes" Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                            }
                        });
                // Setting Negative "NO" Button
                alertDialog.setNegativeButton("NO",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                // Write your code here to execute after dialog
                                Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
                                dialog.cancel();
                            }
                        });

                // Showing Alert Message
                alertDialog.show();

            }
        });

        /**
         * Showing Alert Dialog with Two Buttons one Positive Button with Label
         * "YES" one Neutral Button with Label "NO" one Negative Button with
         * label "Cancel"
         */
        btnAlertThreeBtns.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // Creating alert Dialog with three Buttons

                AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        MainActivity.this);

                // Setting Dialog Title
                alertDialog.setTitle("Save File...");

                // Setting Dialog Message
                alertDialog.setMessage("Do you want to save this file?");

                // Setting Icon to Dialog
                alertDialog.setIcon(R.drawable.save);

                // Setting Positive Yes Button
                alertDialog.setPositiveButton("YES",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // User pressed Cancel button. Write Logic Here
                                Toast.makeText(getApplicationContext(),
                                        "You clicked on YES",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                // Setting Positive Yes Button
                alertDialog.setNeutralButton("NO",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // User pressed No button. Write Logic Here
                                Toast.makeText(getApplicationContext(),
                                        "You clicked on NO", Toast.LENGTH_SHORT)
                                        .show();
                            }
                        });
                // Setting Positive "Cancel" Button
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                                int which) {
                                // User pressed Cancel button. Write Logic Here
                                Toast.makeText(getApplicationContext(),
                                        "You clicked on Cancel",
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                // Showing Alert Message
                alertDialog.show();
            }
        });
    }
}

Download Code

Happy Coding ūüôā