Browse By

Alert Dialog box in Android

Dialog box used to show some information or prompt  user to do some kind of action. In Android, we have many different  dialog types. Each has special function that most users should be somewhat familiar with it. In Pulse 7 we will discuss below dialog box in depth.

  1.  Dialog
  2. Alert Dialog
  3. Progress Dialog
  4. Date Picker Dialog
  5. Time Picker Dialog

In this tutorial, we will learn how to show Alert Dialog box in Android Application. We will use AlertDialog.Builder to create AlertDialog and will set Title , Message , Icons and Button like (Ok, Yes, No and Cancel ). I will give you code snippets for each alert dialog.

Direct Download Full Source Code

 

 

Alert Dialog box in Android

Let first see how to create simple alert with “Ok” button. In this code important methods are :

              • setTitle() used for setting Title in Alert Dialog
              • setMessage() used for setting Message in Alert Dialog
              • setIcon() used for setting Icon in Alert Dialog
              • setButton() used for setting Button in Alert Dialog, we will set “OK” button.
                AlertDialog alertDialog = new AlertDialog.Builder(this).create();
                
                			// Setting Dialog Title
                			alertDialog.setTitle("Simple Alert Dialog");
                
                			// Setting Dialog Message
                			alertDialog.setMessage("Welcome to Pulse7.net");
                
                			// Setting Icon to Dialog
                			alertDialog.setIcon(R.drawable.success);
                
                			// Setting OK Button
                			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                				public void onClick(DialogInterface dialog, int which) {
                					// Write your code here to execute after dialog closed
                					Toast.makeText(getApplicationContext(),
                							"You clicked on OK", Toast.LENGTH_SHORT).show();
                				}
                			});
                
                			// Showing Alert Message
                			alertDialog.show();

                Below snaps gives you idea how above code works.

                Simple Alert with OK Button

                Simple Alert with OK Button 

Lets now see how to create Alert Dialog with two buttons “Yes” and “No”.  We will prompt delete confirmation with this alert dialog. Please note this time we will use AlertDialog.Builder instead of simple AlertDialog class to build Alert Dialog . In this code important methods are :

    • setPositiveButton() used to set Positive button like “Yes” in alert dialog.

 

  • setNegativeButton() used to set Negative button like “No” in alert dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

			// Setting Dialog Title
			alertDialog.setTitle("Confirm Delete...");

			// Setting Dialog Message
			alertDialog.setMessage("Are you sure you want delete this?");

			// Setting Icon to Dialog
			alertDialog.setIcon(R.drawable.delete);

			// Setting Positive "Yes" Button
			alertDialog.setPositiveButton("YES",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {

							// Write your code here to invoke YES event
							Toast.makeText(getApplicationContext(),
									"You clicked on YES", Toast.LENGTH_SHORT)
									.show();
						}
					});

			// Setting Negative "NO" Button
			alertDialog.setNegativeButton("NO",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							// Write your code here to invoke NO event
							Toast.makeText(getApplicationContext(),
									"You clicked on NO", Toast.LENGTH_SHORT)
									.show();
							dialog.cancel();
						}
					});

			// Showing Alert Message
			alertDialog.show();

Below snaps gives you idea how above code works.

Alert Dialog with "Yes" and "No" Button

Alert Dialog with “Yes” and “No” Button

Now lets see how to create AlertDialog with 3 Buttons “Yes” , “No” and “Cancel”.  We will prompt your for saving file. In this code important method is :

  • setNeutralButton() allow you to set Neutral Button like “Cancel”.
    			AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    
    			// Setting Dialog Title
    			alertDialog.setTitle("Save File...");
    
    			// Setting Dialog Message
    			alertDialog.setMessage("Do you want to save this file?");
    
    			// Setting Icon to Dialog
    			alertDialog.setIcon(R.drawable.save);
    
    			// Setting Positive "Yes" Button
    			alertDialog.setPositiveButton("YES",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog, int which) {
    							// User pressed YES button. Write Logic Here
    							Toast.makeText(getApplicationContext(),
    									"You clicked on YES", Toast.LENGTH_SHORT)
    									.show();
    						}
    					});
    
    			// Setting Negative "NO" Button
    			alertDialog.setNegativeButton("NO",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog, int which) {
    							// User pressed No button. Write Logic Here
    							Toast.makeText(getApplicationContext(),
    									"You clicked on NO", Toast.LENGTH_SHORT)
    									.show();
    						}
    					});
    
    			// Setting Netural "Cancel" Button
    			alertDialog.setNeutralButton("Cancel",
    					new DialogInterface.OnClickListener() {
    						public void onClick(DialogInterface dialog, int which) {
    							// User pressed Cancel button. Write Logic Here
    							Toast.makeText(getApplicationContext(),
    									"You clicked on Cancel", Toast.LENGTH_SHORT)
    									.show();
    						}
    					});
    
    			// Showing Alert Message
    			alertDialog.show();

Below snaps gives you idea how above code works.

Alert Dialog with Three button, "Yes", "No" and "Cancel"

Alert Dialog with Three button, “Yes”, “No” and “Cancel”

I hope you like this article. Share your views to to improve content.

Download Full Source Code