Alert Dialog box in android

In this example will see how to display an Alert Dialog box in Android using Android Studio.

Alert Dialog box  is a small window which pops out on a screen to display particular events. This helps to draw users attention before moving further in the application. Alert dialog also comes with pre defined components. It usually have two buttons one is positive (OK) and another one is Negative  (Cancel) button. You can also define different actions on buttons clicks.

You can also create your own Custom Alert Box.

In this tutorial we will be creating two different Alert Dialog boxes. We need to create an object of AlertDialog Builder, which is inner class of AlertDialog to display the dialog box.

Few Methods names and Description

AlertDialog.Builder builder = new AlertDialog.Builder(this);
This method used to set icon to Alert Dialog.setIcon(Drawable icon) – builder.setIcon(R.drawable.icon);
This method is used to display content on dialog windowsetMessage(CharSequence message) – builder.setMessage(“Select you gender”);
This method is used to set title to your dialog windowsetTitle(CharSequence title) – builder.setTitle(“Android Aura”);
This method is used to set if dialog box should cancel or not if user clicks outside the windowsetCancelable(boolean cancelable) – builder.setCancelable(false);
This method will dismiss the dialog windowdismiss();
This method will hide the dialog boxhide();

In the given example below we will use one button. We will call method to display alert dialog box. Dialog box will open with message “Hello this is AlertDialog – Press OK to close”. Once you click on  “OK” button than dialog box will dismiss. Here we use dialog.cancel() method to dismiss the dialog window.

Example of Alert Dialog box in android

// MainActivity.java

package com.androidaura.alertdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        displayalert();
    }

    public void displayAlert() {

        new AlertDialog.Builder(MainActivity.this)
                .setTitle("Title")
                .setMessage("Hello this is AlertDialog - Press OK to close")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                }).show();
    }
} 

Alert Dialog

In the second example we will ask user to select their gender. Here we will use two buttons Positive and Negative. Once user clicks on Positive button than “Male” selected Toast occurs and if users clicks on Negative button than “Female” selected message will
Toast.

public void displayalert() {
       new AlertDialog.Builder(MainActivity.this)
              .setTitle("Title")
              .setMessage("Gender ?")
             //.setIcon(R.drawable.image)
                      .setPositiveButton("Male",
                     new DialogInterface.OnClickListener() {

                         public void onClick(DialogInterface dialog, int id) {
                             Toast.makeText(MainActivity.this,"Male Selected",Toast.LENGTH_SHORT).show();
                              dialog.cancel();
                            }
                        })
          .setNegativeButton("Female", new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(MainActivity.this,"Female Selected",Toast.LENGTH_SHORT).show();
                        dialog.cancel();
                    }
                }).show();
    } 
Output Alert Dialog