AlertDialog In Android Application

In this article, I will explain how to use AlertDialog box in Android app. It is used to display the dialog message with Yes and No or OK and Cancel buttons. It can be used to ask the user about his/her choice to continue or discontinue.

There are three main components for the AlertDialog,

  1. Title
  2. Content Area (Message)
  3. Action buttons (Yes and No or OK and Cancel) 

So to make an alert dialog, we need to make an object of AlertDialogBuilder which is an inner class of AlertDialog while Android AlertDialog is the subclass of Dialog class and syntax is given below to create an alert dialog.

  1. AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);   
So, I created a new project with a blank activity called MainActivity.

 
After creating project MainActivity.java will look like the following:

 

1. Activity_main.xml

 In this file, here we have  only a Button.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.   
  3.      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  4.      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  5.      android:paddingRight="@dimen/activity_horizontal_margin"  
  6.      android:paddingTop="@dimen/activity_vertical_margin"  
  7.      android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  8.    
  9.      <Button  
  10.          android:layout_width="wrap_content"  
  11.          android:layout_height="wrap_content"  
  12.          android:text="Alert Dialog"  
  13.          android:id="@+id/alertButton"  
  14.          android:layout_marginTop="91dp"  
  15.          android:layout_marginLeft="80dp"  
  16.          android:onClick="openalertdialog"  
  17.          />   
  18.  </RelativeLayout>  
2. Activity Class

Here is the code to create and show the AlertDialog.

  1. package com.example.administrator.alertdialogapp;  
  2. import android.content.DialogInterface;  
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.os.Bundle;  
  5. import android.support.v7.app.AlertDialog;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.Toast;  
  10. public class MainActivity extends ActionBarActivity  
  11. {  
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState)  
  14.     {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.activity_main);  
  17.     }  
  18.     public void openalertdialog(View vw)  
  19.     {  
  20.         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);  
  21.         //Set title of Alert Dialog  
  22.         alertDialogBuilder.setTitle("Close the App");  
  23.         //Set dialog message  
  24.         alertDialogBuilder.setMessage("Do u want to quit from the app?");  
  25.         alertDialogBuilder.setCancelable(false);  
  26.         alertDialogBuilder.setPositiveButton("yes"new DialogInterface.OnClickListener()  
  27.         {  
  28.             @Override  
  29.             public void onClick(DialogInterface dialogInterface, int i)  
  30.             {  
  31.                 Toast.makeText(MainActivity.this"You quit from the app!", Toast.LENGTH_LONG).show();  
  32.                 finish();  
  33.             }  
  34.         });  
  35.         alertDialogBuilder.setNegativeButton("no"new DialogInterface.OnClickListener()  
  36.         {  
  37.             @Override  
  38.             public void onClick(DialogInterface dialogInterface, int i)  
  39.             {  
  40.                 Toast.makeText(MainActivity.this"You are still in the app!", Toast.LENGTH_LONG).show();  
  41.             }  
  42.         });  
  43.         AlertDialog alertDialog = alertDialogBuilder.create();  
  44.         alertDialog.show();  
  45.     }  
  46.     @Override  
  47.     public boolean onCreateOptionsMenu(Menu menu)  
  48.     {  
  49.         // Inflate the menu; this adds items to the action bar if it is present.  
  50.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  51.         return true;  
  52.     }  
  53.     @Override  
  54.     public boolean onOptionsItemSelected(MenuItem item)  
  55.     {  
  56.         // Handle action bar item clicks here. The action bar will  
  57.         // automatically handle clicks on the Home/Up button, so long  
  58.         // as you specify a parent activity in AndroidManifest.xml.  
  59.         int id = item.getItemId();  
  60.         //noinspection SimplifiableIfStatement  
  61.         if(id == R.id.action_settings)  
  62.         {  
  63.             return true;  
  64.         }  
  65.         return super.onOptionsItemSelected(item);  
  66.     }  
  67. }   

Run the application on the mobile device and see the output.

 

Click on Alert Dialog button and you will see two options YES and NO.

 

Select No to still exist in the app.

 

Otherwise, quit from the app.
 
 

Read more articles on Android:

Up Next
    Ebook Download
    View all
    Learn
    View all