Android AlertDialog Example

0

In this article, I will show you an example of AlertDialog.

We will show an alert dialog when we want the user to make a decision. AlertDialog is the class that represents an alert dialog. To build it, you need to use  AlertDialog.Builder.

Let’s create a simple alert dialog and show it once user clicks on a button.

Steps to Build AlertDialog

  1. Create an instance of AlertDialog.Builder.
  2. Configure the dialog using the builder instance.
  3. Set the dialog attributes like title, message, icon. For example, to set title call setTitle.
  4. Add the action buttons which will define the action to take when the user presses the button.
  5. Show the dialog.

Main Layout

Main layout consists of a text field and a button. When the button is clicked, we want to show the alert dialog.

welcome.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.javarticles.android.AlertDialogExampleActivity" >

    <TextView
        android:id="@+id/welcome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="18dp"
        android:text="@string/welcome"
        android:textColor="@color/welcome_text_color"
        android:textSize="20sp" />

    <Button
        android:id="@+id/open_alert_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/welcome"
        android:layout_below="@+id/welcome"
        android:layout_marginLeft="48dp"
        android:layout_marginTop="30dp"
        android:text="@string/alert_dialog_btn" />

</RelativeLayout>

Main Activity

In the main activity, we will configure the button so that when clicked, it builds the alert dialog and displays it. Few points to note about:

  1. Use an instance of AlertDialog.Builder to build the AlertDialog.
  2. Call AlertDialogBuilder.create() to create AlertDialog.
  3. Call AlertDialog.show() to display the alert dialog.
  4. You can also directly call AlertDialogBuilder.show() to display the dialog

To add action buttons Ok and Cancel, call the setPositiveButton() and setNegativeButton() methods. Pass an instance of DialogInterface.OnClickListener to define the action to be performed when user presses the button.

In our example, when user presses ‘Ok’, we want to open another screen. When ‘Cancel’ is pressed, we will simply display a message.

Use neutral when you want to proceed with an action which is neither positive nor negative buttons. In our case, when the neutral button ‘Exist’ is pressed, we will finish with our activity.

AlertDialogExampleActivity:

package com.javarticles.android;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class AlertDialogExampleActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome);
		Button alertBtn = (Button) findViewById(R.id.open_alert_btn);
		alertBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showAlertDialog();
			}
		});
	}
	
	private void showAlertDialog() {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
        .setTitle(R.string.alert_dialog_title)
        .setMessage(R.string.alert_dialog_message)
        .setIcon(android.R.drawable.ic_dialog_alert)
        .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(AlertDialogExampleActivity.this, ConfigurationActivity.class);
				startActivity(intent);
			}})
	    .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(AlertDialogExampleActivity.this, "Canceled", Toast.LENGTH_SHORT).show();
				
			}})
			.setNeutralButton(R.string.exit, new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
		
		alertDialog.show();
		
	}
		
}

Run the application

Alert Dialog to Configure.

alert_dialog

Alert Dialog to Configure

Click on Cancel to close the alert.

Cancel

On Cancel

Click on Ok to navigate to Configuration screen.

Ok

On Ok

Click on Exist to finish with the activity.

Neutral

Neutral Case

Download the Source Code

This was an example of AlertDialog. You can download the source code here: alertDialogExample.zip

Share.

Comments are closed.