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“.

Steps:

  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.

 Now Coming to the Java File (AlertDialogExample.java)

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

 

alert-dialog-in-android

download

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 onNext Page Button below.

 

Post By SmrutiRanjan (57 Posts)

Working @ Samsung as a Project Lead for Android Smartphones. I have been blogging since 2008. Previously I was writing articles for other bloggers, but finally I have started my own blog-"Techblogon".I am also an active contributor for the blog-"Gadgets n Gizmos World". Job is my necessity, but blogging is my passion.

Website: → Techblogon

Connect