Sunday, November 04, 2012

How to create alert dialog box in Android

In this post, I am going to explain about how to create alert dialog in android application. In dialog box interface, you can set the text for title, Yes & No button. And also, you can define the action for each button events.

Please use the below code to create alert dialog in button click event.

 btnAlertDialog.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);  
                     // Title of Alert Dialog  
                     alertDialogBuilder.setTitle("Your Title");  
                     // Building alert dialog  
                     alertDialogBuilder  
                          .setMessage("Welcome Ayyanar!.")  
                          .setCancelable(false)   
                          .setPositiveButton("Yes",new DialogInterface.OnClickListener() {  
                          public void onClick(DialogInterface dialog,int id) {  
                                    txtAlertText.setText("OK Thanks!");  
                               }  
                           })  
                          .setNegativeButton("No",new DialogInterface.OnClickListener() {  
                          public void onClick(DialogInterface dialog,int id) {  
                                    txtAlertText.setText("No Thanks!");  
                     // Close the dialog  
                                    dialog.cancel();  
                         }  
                       });  
                     // create alert dialog  
                     AlertDialog alertDialog = alertDialogBuilder.create();  
                     // open dialog  
         alertDialog.show();  
              }  
             }  
        );  


From the above code, you can able to understand on how to set text and event for each button. Everything self descriptive.

Here is the complete code for alert dialog example.

Layout File:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent" >  
   <TextView  
     android:id="@+id/txtAlertText"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_centerHorizontal="true"  
     android:layout_centerVertical="true"  
     android:padding="@dimen/padding_medium"  
     android:text="[Alert Text]"  
     tools:context=".AlertDialogActivity" />  
   <Button  
     android:id="@+id/btnOpenAlertDialog"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_above="@+id/txtAlertText"  
     android:layout_centerHorizontal="true"  
     android:layout_marginBottom="56dp"  
     android:text="Open Alert Dialog" />  
 </RelativeLayout>  



Activity Class:

 import android.os.Bundle;  
 import android.app.Activity;  
 import android.app.AlertDialog;  
 import android.content.Context;  
 import android.content.DialogInterface;  
 import android.view.Menu;  
 import android.view.View;  
 import android.view.View.OnClickListener;  
 import android.widget.Button;  
 import android.widget.TextView;  
 public class AlertDialogActivity extends Activity {  
      final Context context = this;  
      Button btnAlertDialog;  
      TextView txtAlertText;  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.alert_dialog);  
     btnAlertDialog = (Button) findViewById(R.id.btnOpenAlertDialog);  
     txtAlertText = (TextView) findViewById(R.id.txtAlertText);  
     btnAlertDialog.setOnClickListener(new OnClickListener() {  
             public void onClick(View v) {  
                     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);  
                     // Title of Alert Dialog  
                     alertDialogBuilder.setTitle("Your Title");  
                     // Building alert dialog  
                     alertDialogBuilder  
                          .setMessage("Welcome Ayyanar!.")  
                          .setCancelable(false)   
                          .setPositiveButton("Yes",new DialogInterface.OnClickListener() {  
                          public void onClick(DialogInterface dialog,int id) {  
                                    txtAlertText.setText("OK Thanks!");  
                               }  
                           })  
                          .setNegativeButton("No",new DialogInterface.OnClickListener() {  
                          public void onClick(DialogInterface dialog,int id) {  
                                    txtAlertText.setText("No Thanks!");  
                     // Close the dialog  
                                    dialog.cancel();  
                         }  
                       });  
                     // create alert dialog  
                     AlertDialog alertDialog = alertDialogBuilder.create();  
                     // open dialog  
         alertDialog.show();  
              }  
             }  
        );  
   }  
   @Override  
   public boolean onCreateOptionsMenu(Menu menu) {  
     getMenuInflater().inflate(R.menu.alert_dialog, menu);  
     return true;  
   }  
 }  



Manifest file:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.ayyanar.helloworld"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
      <activity  
       android:name=".AlertDialogActivity"  
       android:label="@string/title_activity_alert_dialog" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  


Screenshots:





No comments: