How to Add AlertDialog in Android

Introduction

 
In this article, we will see how to add an AlertDialog in Android.
 
Procedure
  1. Start the Eclipse IDE.
  2. Create a new project.
  3. Create a MainActivity.java file.
  4. Create an activity_main.xml file for layout design.
  5. Add a button in the XML layout.
  6. Then look up the button by its id in the MainActivity.java file.
  7. Create an object like this: AlertDialog.Builder a1=new AlertDialog.Builder(this);
The code for the example is given below.
 
MainActivity.java
  1. package com.example.aadialogtodayyy;    
  2. import android.app.Activity;    
  3. import android.app.AlertDialog;    
  4. import android.content.DialogInterface;    
  5. import android.content.DialogInterface.OnClickListener;    
  6. import android.os.Bundle;    
  7. import android.os.Process;    
  8. import android.view.KeyEvent;    
  9. import android.view.View;    
  10. import android.widget.Button;    
  11.     
  12. public class MainActivity extends Activity implements android.view.View.OnClickListener    
  13. {    
  14.     Button b1;    
  15.     @Override    
  16.     protected void onCreate(Bundle savedInstanceState) {    
  17.     // TODO Auto-generated method stub    
  18.     super.onCreate(savedInstanceState);    
  19.     setContentView(R.layout.activity_main);    
  20.     b1=(Button) findViewById(R.id.button1);    
  21.     b1.setOnClickListener(this);    
  22. }    
  23. public void adddddd()    
  24. {    
  25.     AlertDialog.Builder a1=new AlertDialog.Builder(this);    
  26.     a1.setTitle("Exit from Abhijeet's Application");    
  27.     a1.setMessage("Are u sure ?");    
  28.    a1.setIcon(getResources().getDrawable(R.drawable.ic_launcher));    
  29.    a1.setPositiveButton("Yes"new OnClickListener() {    
  30.     
  31. public void onClick(DialogInterface dialog, int which) {    
  32.     // TODO Auto-generated method stub    
  33.     finish();    
  34.     }    
  35. });    
  36. a1.setNegativeButton("No"new OnClickListener() {    
  37.     public void onClick(DialogInterface dialog, int which) {    
  38.         // TODO Auto-generated method stub    
  39.        Process.killProcess(Process.myPid());    
  40.     }    
  41. });    
  42. a1.show();    
  43. }    
  44. public void onClick(View v) {    
  45.     // TODO Auto-generated method stub    
  46.     adddddd();    
  47. }    
  48. @Override    
  49. public boolean onKeyDown(int keyCode, KeyEvent event) {    
  50.     if(keyCode==KeyEvent.KEYCODE_MENU)    
  51.    {    
  52.        adddddd();    
  53.    }    
  54.     // TODO Auto-generated method stub    
  55.     return super.onKeyDown(keyCode, event);    
  56.   }    
  57. }   
Activity_main.xml
  1. <RelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.   
  5. android:layout_width="match_parent"  
  6. android:layout_height="match_parent"  
  7. android:paddingBottom="@dimen/activity_vertical_margin"  
  8. android:paddingLeft="@dimen/activity_horizontal_margin"  
  9. android:paddingRight="@dimen/activity_horizontal_margin"  
  10. android:paddingTop="@dimen/activity_vertical_margin"  
  11. tools:context=".MainActivity" >  
  12.       
  13. <Button  
  14.   android:id="@+id/button1"  
  15.   android:layout_width="wrap_content"  
  16.   android:layout_height="wrap_content"  
  17.   android:layout_alignParentLeft="true"  
  18.   android:layout_alignParentTop="true"  
  19.   android:layout_marginLeft="106dp"  
  20.   android:layout_marginTop="52dp"  
  21.   android:text="Click Here for Exit" />  
  22.     
  23. </RelativeLayout> 
Output
 
Output
 
AlertDialog in Android

Recommended Ebook

Printing in C# Made Easy

Download Now!
Similar Articles