AlertDialog

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Android AlertDialog Example

android alert dialog

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue.

Android AlertDialog is composed of three regions: title, content area and action buttons.

Android AlertDialog is the subclass of Dialog class.

Android AlertDialog Example

Let's see a simple example of android alert dialog.

activity_main.xml

You can have multiple components, here we are having only a textview.

File: activity_main.xml
 
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_centerHorizontal="true"  
  11.         android:layout_centerVertical="true"  
  12.         android:text="@string/hello_world" />  
  13.   
  14. </RelativeLayout>  

strings.xml

Optionally, you can store the dialog message and title in the strings.xml file.

File: strings.xml
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.   
  4.     <string name="app_name">alertdialog</string>  
  5.     <string name="hello_world">Hello world!</string>  
  6.     <string name="menu_settings">Settings</string>  
  7.     <string name="dialog_message">Welcome to Alert Dialog</string>  
  8.    
  9.    <string name="dialog_title">Javatpoint Alert Dialog</string>  
  10. </resources>  

Activity class

Let's write the code to create and show the AlertDialog.

  1. package com.example.alertdialog;  

  2.   

  3. import android.os.Bundle;  

  4. import android.app.Activity;  

  5. import android.app.AlertDialog;  

  6. import android.content.DialogInterface;  

  7. import android.view.Menu;  

  8.   

  9. public class MainActivity extends Activity {  

  10.   

  11.     @Override  

  12.     protected void onCreate(Bundle savedInstanceState) {  

  13.         super.onCreate(savedInstanceState);  

  14.           

  15.         AlertDialog.Builder builder = new AlertDialog.Builder(this);  

  16.         //Uncomment the below code to Set the message and title from the strings.xml file  

  17.         //builder.setMessage(R.string.dialog_message) .setTitle(R.string.dialog_title);  

  18.           

  19.         //Setting message manually and performing action on button click  

  20.         builder.setMessage("Do you want to close this application ?")  

  21.             .setCancelable(false)  

  22.             .setPositiveButton("Yes", new DialogInterface.OnClickListener() {  

  23.                 public void onClick(DialogInterface dialog, int id) {  

  24.                 finish();  

  25.                 }  

  26.             })  

  27.             .setNegativeButton("No", new DialogInterface.OnClickListener() {  

  28.                 public void onClick(DialogInterface dialog, int id) {  

  29.                 //  Action for 'NO' Button  

  30.                 dialog.cancel();  

  31.              }  

  32.             });  

  33.   

  34.         //Creating dialog box  

  35.         AlertDialog alert = builder.create();  

  36.         //Setting the title manually  

  37.         alert.setTitle("AlertDialogExample");  

  38.         alert.show();  

  39.         setContentView(R.layout.activity_main);  

  40.     }  

  41.   

  42.     @Override  

  43.     public boolean onCreateOptionsMenu(Menu menu) {  

  44.         // Inflate the menu; this adds items to the action bar if it is present.  

  45.         getMenuInflater().inflate(R.menu.activity_main, menu);  

  46.         return true;  

  47.     }  

  48.   

  49. }  

Output

android alert dialog example output 1