Creating an Alert Dialog Box in Android

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.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="19" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.alertdialog.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26.   
  27. </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.       
  6.     <EditText   
  7.         android:id="@+id/EditText01"   
  8.         android:text="I Love Android"  
  9.     android:layout_height="50dip"   
  10.     android:layout_width="180dip">  
  11.           
  12.     </EditText>  
  13.       
  14.     <Button   
  15.     android:id="@+id/Button01"   
  16.     android:layout_width="wrap_content"  
  17.     android:layout_height="wrap_content"   
  18.     android:layout_below="@+id/EditText01"  
  19.     android:text="Clear Screen">  
  20.           
  21.     </Button>  
  22. </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.   
  3. import android.support.v7.app.ActionBarActivity;  
  4. import android.support.v7.app.ActionBar;  
  5. import android.support.v4.app.Fragment;  
  6. import android.app.AlertDialog;  
  7. import android.app.Dialog;  
  8. import android.content.DialogInterface;  
  9. import android.os.Bundle;  
  10. import android.view.LayoutInflater;  
  11. import android.view.Menu;  
  12. import android.view.MenuItem;  
  13. import android.view.View;  
  14. import android.view.ViewGroup;  
  15. import android.widget.Button;  
  16. import android.widget.EditText;  
  17. import android.widget.Toast;  
  18. import android.os.Build;  
  19.   
  20. public class MainActivity extends ActionBarActivity 
  21. {  
  22.        Button Clear;  
  23.        EditText edit_data;  
  24.        //Called when the activity is first created  
  25.        @Override  
  26.        public void onCreate(Bundle savedInstanceState) 
  27.        {  
  28.              super.onCreate(savedInstanceState);  
  29.              setContentView(R.layout.main);  
  30.        
  31.              Clear=(Button)findViewById(R.id.Button01);  
  32.              edit_data=(EditText)findViewById(R.id.EditText01);  
  33.        
  34.              Clear.setOnClickListener(new View.OnClickListener() 
  35.              {  
  36.           
  37.                    @Override  
  38.                    public void onClick(View v) 
  39.                    {  
  40.               
  41.                         AlertDialog.Builder builder=new AlertDialog.Builder(v.getContext());  
  42.                         //Set a title  
  43.                         builder.setTitle("Confirm");  
  44.                         //Set a message  
  45.                         builder.setMessage("Do you want to Clear?");  
  46.                         ////Here Set a listener to be called when the positive button of the dialog is pressed.  
  47.                         builder.setPositiveButton("OK",new DialogInterface.OnClickListener()
  48.                         {  
  49.                
  50.                               @Override  
  51.                                public void onClick(DialogInterface dialog, int which)
  52.                               {  
  53.                          
  54.                                     //clearing the contents of edittext on click of OK button  
  55.                                     edit_data.setText("");  
  56.                                     //Displaying a toast message  
  57.                                     Toast.makeText(getApplicationContext(), "Your text has been cleared", Toast.LENGTH_LONG).show();  
  58.                               }  
  59.  
  60.                          });  
  61.                         //Here Set a listener to be called when the negative button of the dialog is pressed.  
  62.                          builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() 
  63.                          {  
  64.        
  65.                               @Override  
  66.                               public void onClick(DialogInterface dialog, int which) 
  67.                               {  
  68.                                      // TODO Auto-generated method stub  
  69.                                      dialog.cancel();  
  70.                               }  
  71.                          });  
  72.              
  73.                          //Create the dialog  
  74.                          AlertDialog alertdialog=builder.create();  
  75.        
  76.                          //show the alertdialog  
  77.                          alertdialog.show();  
  78.        
  79.                    }  
  80.              });  
  81.        }  
  82.       
  83. }  

Summary

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


Similar Articles