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,
- 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>
2. Activity Class
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);
-
- alertDialogBuilder.setTitle("Close the App");
-
- 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)
- {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
-
-
-
- int id = item.getItemId();
-
- if(id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
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: