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. public class MainActivity extends Activity implements android.view.View.OnClickListener
  12. {
  13. Button b1;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. // TODO Auto-generated method stub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_main);
  19. b1=(Button) findViewById(R.id.button1);
  20. b1.setOnClickListener(this);
  21. }
  22. public void adddddd()
  23. {
  24. AlertDialog.Builder a1=new AlertDialog.Builder(this);
  25. a1.setTitle("Exit from Abhijeet's Application");
  26. a1.setMessage("Are u sure ?");
  27. a1.setIcon(getResources().getDrawable(R.drawable.ic_launcher));
  28. a1.setPositiveButton("Yes", new OnClickListener() {
  29. public void onClick(DialogInterface dialog, int which) {
  30. // TODO Auto-generated method stub
  31. finish();
  32. }
  33. });
  34. a1.setNegativeButton("No", new OnClickListener() {
  35. public void onClick(DialogInterface dialog, int which) {
  36. // TODO Auto-generated method stub
  37. Process.killProcess(Process.myPid());
  38. }
  39. });
  40. a1.show();
  41. }
  42. public void onClick(View v) {
  43. // TODO Auto-generated method stub
  44. adddddd();
  45. }
  46. @Override
  47. public boolean onKeyDown(int keyCode, KeyEvent event) {
  48. if(keyCode==KeyEvent.KEYCODE_MENU)
  49. {
  50. adddddd();
  51. }
  52. // TODO Auto-generated method stub
  53. return super.onKeyDown(keyCode, event);
  54. }
  55. }
Activity_main.xml
  1. <RelativeLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:paddingBottom="@dimen/activity_vertical_margin"
  7. android:paddingLeft="@dimen/activity_horizontal_margin"
  8. android:paddingRight="@dimen/activity_horizontal_margin"
  9. android:paddingTop="@dimen/activity_vertical_margin"
  10. tools:context=".MainActivity" >
  11. <Button
  12. android:id="@+id/button1"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_alignParentLeft="true"
  16. android:layout_alignParentTop="true"
  17. android:layout_marginLeft="106dp"
  18. android:layout_marginTop="52dp"
  19. android:text="Click Here for Exit" />
  20. </RelativeLayout>
Output
Output
AlertDialog in Android