Introduction
In this article, I will explain how to use the AlertDialog box in the 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,
- Title
- Content Area (Message)
- 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.
- 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.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
- android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Alert Dialog"
- android:id="@+id/alertButton"
- android:layout_marginTop="91dp"
- android:layout_marginLeft="80dp"
- android:onClick="openalertdialog"
- />
- </RelativeLayout>
Here is the code to create and show the AlertDialog.
- package com.example.administrator.alertdialogapp;
- import android.content.DialogInterface;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.support.v7.app.AlertDialog;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.widget.Toast;
- public class MainActivity extends ActionBarActivity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void openalertdialog(View vw)
- {
- AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
- //Set title of Alert Dialog
- alertDialogBuilder.setTitle("Close the App");
- //Set dialog message
- alertDialogBuilder.setMessage("Do u want to quit from the app?");
- alertDialogBuilder.setCancelable(false);
- alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialogInterface, int i)
- {
- Toast.makeText(MainActivity.this, "You quit from the app!", Toast.LENGTH_LONG).show();
- finish();
- }
- });
- alertDialogBuilder.setNegativeButton("no", new DialogInterface.OnClickListener()
- {
- @Override
- public void onClick(DialogInterface dialogInterface, int i)
- {
- Toast.makeText(MainActivity.this, "You are still in the app!", Toast.LENGTH_LONG).show();
- }
- });
- AlertDialog alertDialog = alertDialogBuilder.create();
- alertDialog.show();
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if(id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Run the application on the mobile device and see the output.



Otherwise, quit from the app.

Read more articles on Android

Mehulkumar ChauhanPosted Mar 11, 2016, 1:14 AM
Nice
Kumaresh RajalingamPosted Mar 9, 2016, 8:39 PM
Nice share
kalu singh raoPosted Mar 8, 2016, 9:07 AM
Nice...
Vignesh ManiPosted Mar 8, 2016, 3:40 AM
Good
Atul RawatPosted Mar 8, 2016, 2:18 AM
nice article sir