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.
-
Alert dialog.
-
Progress Dialog
-
Custom Dialog with EditText
-
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“.
Steps:
- First, we will use the
AlertDialog.Builderdefaul android class to create the alert box interface for title, message to display, buttons, and button onClick function - Then We will attach above builder to
AlertDialog - Then we will display it.
- 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click on Show Alert Dialog to show the Alert Dialog" /> <Button android:id="@+id/button_show_alert_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Show Alert Dialog" /> </LinearLayout> |
Now Coming to the Java File (AlertDialogExample.java)
Here we will find the complete java code for Android Alert Dialog.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package com.techblogon.alertdialog; 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.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; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button_show_alert_dialog); //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. AlertDialogExample.this.finish(); } }); alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // Canceled. dialog.cancel(); } }); AlertDialog alertDialog = alert.create(); alertDialog.show(); /* Alert Dialog Code End*/ } }); } } |
![]()
You can also download complete code from here Alert Dialog Example in Android here.
That’s It. Enjoy your Alert Dialog in Android. For other types of Alert Dialog, You can click Here or click on “Next Page“ Button below.








Previous Page
Android Tutorial Home Page









I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon". Techblogon is a technology blog. We regularly update this blog with really nice and helpful tips n tricks, latest updates on new technologies. I am also an active contributor for the blog-"Gadgets and Gizmos World".
Job is my necessity, but blogging is my passion.
Connect with me on