Thursday, August 7, 2014

Use Alert Dialog Box in Android

An alert box can be used to show a warning message to the user or confirm user action. In Android, we can use “android.app.AlertDialog” class to render an alert dialog box.

In this tutorial, we shall see how to display an alert box in Android. We would follow the steps written below :
i. Use the AlertDialog.Builder to create an alert box interface- title, message to be displayed, buttons, and button onclick function.
ii. Attach the above builder to AlertDialog and display it.

1. Add button in XML Layout

Open the "main.xml" file and insert a normal button. When clicked, it should show an Alert dialog box.
File: res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <Button android:id="@+id/buttonAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Box" />
</LinearLayout>

2. Java Code

Set the alert message and the title to be displayed via the "Main.java" file.
File: Main.java

package com.endeavour.tutorial;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.main);
       button = (Button) findViewById(R.id.buttonAlert);
              // add button listener
       button.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View arg0) {
           AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                     // set title
           alertDialogBuilder.setTitle("The Title Goes Here");
                     // set dialog message
           alertDialogBuilder.setMessage("Do you wish to exit!")
                     .setCancelable(false)
                     .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog,int id) {
                              // if this button is clicked, close current activity
                                Main.this.finish();
                           }
                       })
                     .setNegativeButton("No",new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog,int id) {
                   // if this button is clicked, just close the dialog box and do nothing
                                  dialog.cancel();
                           }
                     });
                           // create alert dialog
                     AlertDialog alertDialog = alertDialogBuilder.create();
                           // show it
                     alertDialog.show();
              }
       });
}
}

3. Output

The following output shall be displayed.

i. The homescreen having the "Show Alert Box" button


ii. The Alert box being displayed when the Button is pressed



No comments:

Post a Comment