Android alert dialog example




Android Alert Dialog is one of the most important and basic component in Android applications.  This dialog is used to show alerts to the users, get confirmation from the users.
This is a sample alert.

There are three parts in the alert dialog.
  •       Title
  •       Content
  •       Action Button(s)
In these three parts, ‘Title’ is an optional part.
Content:
This part is used to show the message to the user. In the sample alert dialog shown above ‘Sample One Action Button Alert’ is the content.
This content can be a string message or a list or any custom layout.
Action Button:
This is one of the important parts in the alert dialog. This is used to interact with the user. In the sample alert dialog shown above ‘OK’ is the action button.
Action buttons can be any one of the below three types;
Positive action button
Negative action button
Neutral action button

You can add all three types of action buttons to the Android alert dialog. It depends on the need. But you cannot add more than three action buttons in an alert dialog. Which means a particular type of action button cannot be added more than once.
So an alert dialog can have maximum of one positive, one negative and one neutral action button.
Normally positive action button is used to accept the action. (OK/YES)
Negative action is used to cancel the action. (NO)
Neutral action is used in neutral situations (May be later)

On newer devices using the Holo theme, the action button order (left to right) is now NEGATIVE - NEUTRAL – POSITIVE.

setPositiveButton(), setNegativeButton(), setNeutralButton() methods are used to add the respective action buttons to the alert dialog. These methods take two arguments.
  1.     Text to be displayed in the action button
  2.     DialogInterface.OnClickListener A method which will be executed on the click of the action button.


Title:
This is an optional part of the Android alert dialog. This is used to show simple messages.



AlertDialog.Builder class is used to create the alert dialog. AlertDialog class is a subclass of Dialog that can display one, two or three buttons.
If you only want to display a String in this dialog box, use the setMessage() method.
setTitle() method is used to add title to the alert dialog.
Calling show() method will make the alert dialog visible to the user.

Based on the number of action buttons available in the alert dialog, we can classify it as One button/two button/three button alert dialog.
In the below example I have shown all three types of alert dialogs.














activity_alert.xml
<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"
    tools:context=".AlertActivity" >

    <Button
        android:id="@+id/one_button_alert"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="25dip"
        android:layout_marginLeft="20dip"
        android:text="One Button Alert" />
    <Button
        android:id="@+id/two_button_alert"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/one_button_alert"
        android:layout_marginLeft="20dip"
        android:text="Two Button Alert" />
    <Button
        android:id="@+id/three_button_alert"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
                android:layout_below="@id/two_button_alert"
        android:layout_marginLeft="20dip"
        android:text="Three Button Alert" />
</RelativeLayout>


AlertActivity.java
package com.example.alertdialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

/**
 * @author Vienna
 *
 */
public class AlertActivity extends Activity {
 private Button button1;
 private Button button2;
 private Button button3;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_alert);
  button1 = (Button) findViewById(R.id.one_button_alert);
  button2 = (Button) findViewById(R.id.two_button_alert);
  button3 = (Button) findViewById(R.id.three_button_alert);

  button1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(
      AlertActivity.this);
    builder.setTitle("Sample Alert");
    builder.setMessage("Sample One Action Button Alert");
    builder.setPositiveButton("OK",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "OK");
     }
    });
    builder.show();
   }
  });
  button2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(
      AlertActivity.this);
    builder.setTitle("Sample Alert");
    builder.setMessage("Sample Two Action Button Alert");
    builder.setNegativeButton("NO",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "NO");
     }
    });
    builder.setPositiveButton("YES",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "YES");
     }
    });
    builder.show();
   }
  });
  button3.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(
      AlertActivity.this);
    builder.setTitle("Sample Alert");
    builder.setMessage("Sample Three Action Button Alert");
    builder.setNegativeButton("NO",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "NO");
     }
    });
    builder.setPositiveButton("YES",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "YES");
     }
    });
    builder.setNeutralButton("CANCEL",
    new DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
      int which) {
      Log.e("info", "CANCEL");
     }
    });

    builder.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.activity_alert, menu);
  return true;
 }

}


Output:





Source code of this sample application:
AlertDialog.zip

 



Reactions:

0 comments :

Post a Comment