Alert Dialog in Android Example with Source Code and Description

Alert Dialog in Android Example with Source Code and Description

Alert Dialog in Android Example with Source Code and Description. Android Dialogs are always created for receiving user’s response and are part of the current displayed Activity. A dialog gets the focus until the user closes it. There are different types of dialogs in android. Trust me guys, Somehow Android dialog code is little lengthy but very easy to understand. Here We will discuss with  Alert Dialog in Android Example with Source Code and Description.

  1. Alert dialog.

  2. Progress Dialog

  3. Custom Dialog with EditText

  4. Other Custom Dialogs

Here we will discuss all types of Android Dialogs one by one.

Alert Dialog in Android

This is one of the simplest dialog in android. Here we will show an Android alert dialog with 2 buttons (YES, NO). When we press YES, the application will close and when we will press CANCEL, the alert dialog will just dismiss. Note: You can give any name to you alert dialog buttons as per your requirement. Here we have created a button called “Show Alert Dialog“. We will show the alert dialog when we will click on the button  “Show Alert Dialog“.


  1. First, we will use the AlertDialog.Builder defaul android class to create the alert box interface for title, message to display, buttons, and button onClick function
  2. Then We will attach above builder to AlertDialog
  3. Then we will display it.
  4. Done.

Complete Code for Alert Dialog in Android

We have used one button for the Alert Dialog  project, so that we can show the Alert Dialog Box when we will click on the button.

  • Create new android project [File >> New >> Android Project] with Project Name: AlertDialogExample
  • Click next and select target android device version [I chose version 2.2]
  • Click next and enter package name – ‘com.example.alertdialog’
  • Click finish

Layout creation:

Default Application layout – main.xml:

Open main.xml under /res/layout and replace it with the below XML.

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout xmlns:android=””
android:text=”Click on Show Alert Dialog to show the Alert Dialog”
android:text=”Show Alert Dialog” />


Now Coming to the Java File (

Here we will find the complete java code for Android Alert Dialog.

package com.techblogon.alertdialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class AlertDialogExample extends Activity {
/** Called when the activity is first created. */

// this context will use when we work with Alert Dialog
final Context context = this;

// just for test, when we click this button, we will see the alert dialog.
private Button button;

public void onCreate(Bundle savedInstanceState) {

button = (Button) findViewById(;
//Create onclick listener class

button.setOnClickListener(new View.OnClickListener() {
//When you click the button, Alert dialog will be showed
public void onClick(View v) {

/* Alert Dialog Code Start*/
AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle(“Alert DIalog With EditText”); //Set Alert dialog title here
alert.setMessage(“Enter your Name Here”); //Message here

alert.setPositiveButton(“OK”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// String value = input.getText().toString();
// Do something with value!
//You will get input data in this variable.


alert.setNegativeButton(“CANCEL”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
AlertDialog alertDialog = alert.create();;
/* Alert Dialog Code End*/


Leave a Reply

Your email address will not be published. Required fields are marked *