Saturday, 21 July 2012

Android Alert Dialog Box Tutorial


Introduction of Alert Dialog Box Control



Situation : Everyone uses javascript confirm() function. This function display a message with user choice to yes or no. Same functionality in android achieved by alert dialog.
Android provides confirm functionality using alert dialog box. For using this feature we need to createAlertDialog.Builder object. In this we pass Current context.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
Using this object we set following properties of this dialog.
  1. setTitle() : This function is used to set title of alert dialog box.

    Syntex :
    
    alertDialogBuilder.setTitle("Your Title");
    
  2. setCancelable () : This functionis used for cancellation of alert dialog box. We can set true or falsevalues.

    Syntex :
    
    alertDialogBuilder.setCancelable(false);
    
  3. setPositiveButton() : This function create a button with desired text. In our syntex("Yes"). If user click this button alert event which is attached to it triggered.

    Syntex :
    
    alertDialogBuilder.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    					
    public void onClick(DialogInterface dialog,int id) {
                //  Task to perform
                
                
    					}
    				});
    
    
  4. setNegativeButton() : This function create a button with desired text. In our syntex("No"). If user click this button alert event which is attached to it triggered or cancel the alert dialog box.

    Syntex :
    
    alertDialogBuilder.setNegativeButton("No",new DialogInterface.OnClickListener() {
    					
    public void onClick(DialogInterface dialog,int id) {
    						
    // if this button is clicked, just close
    						
    // the dialog box and do nothing
    						
    dialog.cancel();
    					}
    				});
    
After setting above properties we use create() method to create alert dialog.
Syntex :

AlertDialog alertDialog = alertDialogBuilder.create();
At last step we show this alert dialog box. using show() method.
Syntex :

alertDialog.show();




Example
  1. Make layout file which contains a button. If we click on button it show alert dialog.

    <?xml version="1.0" encoding="utf-8"?>
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
        android:layout_width="fill_parent"
    
        android:layout_height="fill_parent"
    
        android:orientation="vertical" >
    
       
    
    
        <Button
    
            android:id="@+id/buttonToast"
    
            android:layout_width="wrap_content"
    
            android:layout_height="wrap_content"
    
            android:text="Show Alert Box" />
    
    </LinearLayout>
    
  2. Then apply onclick event in mainactivity.class

    public class MainActivity extends Activity {
     
    	final Context context = this;
    	private Button button;
     
    	public void onCreate(Bundle savedInstanceState) {
     
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.main);
     
    		button = (Button) findViewById(R.id.buttonAlert);
     
    		// add button listener
    		button.setOnClickListener(new OnClickListener() {
     
    		@Override
    		public void onClick(View arg0) {
     
    			
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
    				context);
     
    			
    // set title
    
      alertDialogBuilder.setTitle("Your Title");
     
    			
    // set dialog message
    			
       alertDialogBuilder.setMessage("Click yes to exit!")
    		     .setCancelable(false)
    		     
    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
    					
    public void onClick(DialogInterface dialog,int id) {
    						
    // if this button is clicked, close
    						
    // current activity
    						
    MainActivity.this.finish();
    					}
    				  })
    				
    .setNegativeButton("No",new DialogInterface.OnClickListener() {
    					
    public void onClick(DialogInterface dialog,int id) {
    						
    // if this button is clicked, just close
    						
    // the dialog box and do nothing
    						
    dialog.cancel();
    					}
    				});
      
    				// create alert dialog
    				
    AlertDialog alertDialog = alertDialogBuilder.create();
     
    				
    // show it
    				
    alertDialog.show();
    			}
    		});
    	}
    }
    
    
  3. Output : 
                 

1 comment: