Android tutorial - Alertdialog in android | Android Alert Dialog box example - android app development - android studio - android development tutorial



Android Alert Dialog:

 android alert dialog box
  • Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.
  • Android AlertDialog is composed of three regions: title, content area and action buttons.
  • Android AlertDialog is the subclass of Dialog class.
  • An alert dialog box is a special dialog box that is displayed in a graphical user interface when something unexpected occurred that requires immediate user action.
  • The typical alert dialog provides information in a separate box to the user, after which the user can only respond in one way: by closing it.
  • Closing an alert dialog will provide access to the original window, which is not available while the alert dialog is presented.
 alert dialog box various options

Alerts have several typical uses:

  • Error: informs the user than an operation could not continue or complete due to some insurmountable error.
  • Warning: alerts that the current course of action could be in some way dangerous or detrimental, often offering the option of not proceeding.
  • Info: presents a general notification about a recent event.
  • Question: elicits some kind of response from the user, required to complete the current process.
  • Below is the following Steps :
    • First, use the AlertDialog.Builder to create the alert box interface, like title, message to display, buttons, and button onclick function
    • Later attach above builder to AlertDialog and display it.
    • Done.

1 Android Layout Files

  • Simple layout file, display a button on screen.
  • 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:orientation="vertical" >

    <Button
        android:id="@+id/buttonAlert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Box" />

</LinearLayout>
click below button to copy the code from android tutorial team

2. Activity

  • When user click on this button, display the alert box, with your pre-defined alert dialog interface.
  • File : MainActivity.java
package com.wikitechy.android;

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 MainActivity 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("Your Title");

			// set dialog message
			alertDialogBuilder
				.setMessage("Click yes to exit!")
				.setCancelable(false)
				.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog,int id) {
						// if this button is clicked, close
						// current activity
						MainActivity.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();
			}
		});
	}
}
click below button to copy the code from android tutorial team

3. Demo - android emulator - android tutorial

  • Start it, display a button.
 alert dialog box
  • When button is clicked, display the alert box
 alert dialog box processing
  • If “Yes” button is clicked, close the activity and return back to your Android main screen.
 android main screen

Related Searches to Alertdialog in android | Android Alert Dialog box example