Introduction

Hey guys how are you? Today I will show you how to make an alert dialog box in Android. In this tutorial, I am using the class reference here Android. Builder. Let's start.
Figure 1
alertDialog in android
Figure 2
enter name in alertDialog in android
Figure 3
clear screen in android alertDialog
AndroidManifest.xml File
This is your AndroidManifest File; replace it with your own:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.alertdialog"
  4. android:versionCode="1"
  5. android:versionName="1.0" >
  6. <uses-sdk
  7. android:minSdkVersion="8"
  8. android:targetSdkVersion="19" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.example.alertdialog.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Main.xml(Layout File)
This is your layout file:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <EditText
  6. android:id="@+id/EditText01"
  7. android:text="I Love Android"
  8. android:layout_height="50dip"
  9. android:layout_width="180dip">
  10. </EditText>
  11. <Button
  12. android:id="@+id/Button01"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:layout_below="@+id/EditText01"
  16. android:text="Clear Screen">
  17. </Button>
  18. </RelativeLayout>
MainActivity.java File
This is your main Java file in which you will code it:
package com.example.alertdialog;
  1. package com.example.alertdialog;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.support.v7.app.ActionBar;
  4. import android.support.v4.app.Fragment;
  5. import android.app.AlertDialog;
  6. import android.app.Dialog;
  7. import android.content.DialogInterface;
  8. import android.os.Bundle;
  9. import android.view.LayoutInflater;
  10. import android.view.Menu;
  11. import android.view.MenuItem;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.EditText;
  16. import android.widget.Toast;
  17. import android.os.Build;
  18. public class MainActivity extends ActionBarActivity
  19. {
  20. Button Clear;
  21. EditText edit_data;
  22. //Called when the activity is first created
  23. @Override
  24. public void onCreate(Bundle savedInstanceState)
  25. {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.main);
  28. Clear=(Button)findViewById(R.id.Button01);
  29. edit_data=(EditText)findViewById(R.id.EditText01);
  30. Clear.setOnClickListener(new View.OnClickListener()
  31. {
  32. @Override
  33. public void onClick(View v)
  34. {
  35. AlertDialog.Builder builder=new AlertDialog.Builder(v.getContext());
  36. //Set a title
  37. builder.setTitle("Confirm");
  38. //Set a message
  39. builder.setMessage("Do you want to Clear?");
  40. ////Here Set a listener to be called when the positive button of the dialog is pressed.
  41. builder.setPositiveButton("OK",new DialogInterface.OnClickListener()
  42. {
  43. @Override
  44. public void onClick(DialogInterface dialog, int which)
  45. {
  46. //clearing the contents of edittext on click of OK button
  47. edit_data.setText("");
  48. //Displaying a toast message
  49. Toast.makeText(getApplicationContext(), "Your text has been cleared", Toast.LENGTH_LONG).show();
  50. }
  51. });
  52. //Here Set a listener to be called when the negative button of the dialog is pressed.
  53. builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
  54. {
  55. @Override
  56. public void onClick(DialogInterface dialog, int which)
  57. {
  58. // TODO Auto-generated method stub
  59. dialog.cancel();
  60. }
  61. });
  62. //Create the dialog
  63. AlertDialog alertdialog=builder.create();
  64. //show the alertdialog
  65. alertdialog.show();
  66. }
  67. });
  68. }
  69. }

Summary

For any queries please comment. I hope you guys will like this article and share this article with the world for sharing knowledge.