Alert Dialog

Operating System
We create an AlertDialog by using its Builder class, whose methods all return an instance of Builder. AN ALERT WITH ONE BUTTON
  1. AlertDialog alertDialog = new AlertDialog.Builder(Dialog.this).create();
We set the alert dialog title
  1. alertDialog.setTitle("Alert Dialog");
We set the alertDialog message
  1. alertDialog.setMessage("Welcome to Android");
We set the alert dialog Icon
  1. alertDialog.setIcon(R.drawable.tick);
Finally we create OK button and set onClick listener to the button
  1. alertDialog.setButton("OK",     new DialogInterface.OnClickListener()
  2. {
  3.  
  4. public void onClick(DialogInterface dialog,     int which)
  5. {
  6.                                                        
  7.   Toast.makeText(getApplicationContext(),"You clicked on OK",
  8.   Toast.LENGTH_SHORT).show();
  9. });
We show the dialog.
  1. alertDialog.show();
AN ALERT WITH TWO BUTTONS You can create three types of buttons:- 1.setPositiveButton Describes an intended action or a confirmation, e.g. “Save” or “Yes” 2.setNeutralButton Describes a “neutral” action such as “Close”. 3.setNegativeButton Describes a cancellation or simply a “No”.
  1. AlertDialog.Builder alertDialog = new AlertDialog.Builder(Dialog.this);
  2. alertDialog.setTitle("Confirm Delete...");
  3. alertDialog.setMessage("Are you sure you want delete this?");
  4. alertDialog.setIcon(R.drawable.delete);
We create the Yes button and set the onClick listener
  1. alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener()
  2. {
  3. public void onClick(DialogInterface dialog,int which)
  4. {
  5.    Toast.makeText(getApplicationContext(), "You clicked on YES",
  6.    Toast.LENGTH_SHORT).show();
  7. });
  8.  
  9. We create the Yes button and set the onClick listener
  10.  
  11. alertDialog.setNegativeButton("NO",new DialogInterface.OnClickListener()
  12. {
  13. public void onClick(DialogInterface dialog,int which)
  14. {
  15.  
  16.   Toast.makeText(getApplicationContext(), "You clicked on NO",
  17.   Toast.LENGTH_SHORT).show();
  18.   dialog.cancel();
  19. }
  20. });
  21. alertDialog.show();
AN ALERT WITH THREE BUTTONS
  1. btnAlertThreeBtns.setOnClickListener(new View.OnClickListener()
  2. {
  3.  
  4.  public void onClick(View arg0)
  5.  {
  6.        
  7.   AlertDialog.Builder alertDialog = new AlertDialog.Builder(Dialog.this);
  8.   alertDialog.setTitle("Save File...");
  9.  
  10.   alertDialog.setMessage("Do you want to save this file?");
  11.   // Setting Icon to Dialog
  12.   alertDialog.setIcon(R.drawable.save);
  13.  
  14.   //Here we set the Yes setPositiveButton
  15.   alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener()
  16.   {
  17.  
  18.         public void onClick(DialogInterface dialog, int which)
  19.         {
  20.                                                                 Toast.makeText(getApplicationContext(),"You clicked on YES",
  21.                                                                                 Toast.LENGTH_SHORT).show();
  22.         }
  23.   });
  24.  
  25. // Setting setNegativeButton No
  26. alertDialog.setNeutralButton("NO",new DialogInterface.OnClickListener()
  27. {
  28.  
  29.  public void onClick(DialogInterface dialog,int which)
  30. {
  31.                                                                 Toast.makeText(getApplicationContext(),"You clicked on NO",
  32.       Toast.LENGTH_SHORT).show();
  33. }
  34. });
  35. alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
  36. {
  37.  
  38.         public void onClick(DialogInterface dialog,int which)
  39.         {
  40.                                                                                                 Toast.makeText(getApplicationContext(),"You clicked on
  41.      Cancel",Toast.LENGTH_SHORT).show();
  42.         }
  43. });                            
  44. alertDialog.show();
Here is the complete source code for the above tutorial Main.xml file
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     android:background="#ffffff"
  7. >
  8.    
  9.     <Button
  10.           android:id="@+id/btnAlert"
  11.                 android:layout_width="match_parent"
  12.                 android:layout_height="wrap_content"
  13.                 android:text="Show Alert"              
  14.              android:layout_marginRight="40dp"
  15.                 android:layout_marginLeft="40dp">
  16.     </Button>
  17.    
  18.     <Button
  19.           android:id="@+id/btnAlertWithTwoBtns"
  20.                 android:layout_width="match_parent"
  21.                 android:layout_height="wrap_content"
  22.                 android:text="Show Alert With Two Buttons"             
  23.                 android:layout_marginRight="40dp"
  24.                 android:layout_marginLeft="40dp"
  25.                 >
  26.     </Button>
  27.    
  28.     <Button
  29.             android:id="@+id/btnAlertWithThreeBtns"
  30.                 android:layout_width="match_parent"
  31.                 android:layout_height="wrap_content"
  32.                 android:text="Show Alert with Three Buttons"
  33.                 android:layout_marginRight="40dp"
  34.                 android:layout_marginLeft="40dp"
  35.                 >
  36.     </Button>
  37. </LinearLayout>
Dialog.java
  1. package com.dialog;
  2. import android.app.Activity;
  3. import android.app.AlertDialog;
  4. import android.content.DialogInterface;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.Toast;
  9.  
  10. public class Dialog extends Activity
  11. {
  12.         @Override
  13.         public void onCreate(Bundle savedInstanceState)
  14.         {
  15.                 super.onCreate(savedInstanceState);
  16.                 setContentView(R.layout.main);
  17.  
  18. Button btnAlert = (Button) findViewById(R.id.btnAlert);
  19. Button btnAlertTwoBtns = (Button)    findViewById(R.id.btnAlertWithTwoBtns);
  20. Button btnAlertThreeBtns = (Button) findViewById(R.id.btnAlertWithThreeBtns);
  21.  
  22.                 btnAlert.setOnClickListener(new View.OnClickListener()
  23.                 {
  24.  
  25.                         public void onClick(View arg0)
  26.                         {
  27.                                 // Creating alert Dialog with one Button
  28.  
  29.                             AlertDialog alertDialog = new
  30.                       AlertDialog.Builder(Dialog.this).create();
  31.  
  32.                             // Setting Dialog Title
  33.                             alertDialog.setTitle("Alert Dialog");
  34.  
  35.                             // Setting Dialog Message
  36.                             alertDialog.setMessage("Welcome to Android");
  37.  
  38.                              // Setting Icon to Dialog
  39.                             alertDialog.setIcon(R.drawable.tick);
  40.  
  41.                                 // Setting OK Button
  42.                                 alertDialog.setButton("OK",     new
  43.                         DialogInterface.OnClickListener()
  44.                                 {
  45.  
  46.                 public void onClick(DialogInterface dialog,int which)
  47.               {
  48.                                                                
  49.          // Write your code here to execute after dialog closed
  50.                                                                 Toast.makeText(getApplicationContext(),"You clicked on OK",
  51.       Toast.LENGTH_SHORT).show();
  52.         }
  53.         });
  54.  
  55.                                 // Showing Alert Message
  56.      alertDialog.show();
  57.  
  58.                         }
  59.                 });
  60.  
  61.                 btnAlertTwoBtns.setOnClickListener(new View.OnClickListener()
  62.                 {
  63.  
  64.                         public void onClick(View arg0)
  65.                         {
  66.                                 // Creating alert Dialog with two Buttons
  67.  
  68.                                 AlertDialog.Builder alertDialog = new
  69.                         AlertDialog.Builder(Dialog.this);
  70.  
  71.                                 // Setting Dialog Title
  72.                                 alertDialog.setTitle("Confirm Delete...");
  73.  
  74.                                 // Setting Dialog Message
  75.                                 alertDialog.setMessage("Are you sure you want
  76.                        delete this?");
  77.  
  78.                                 // Setting Icon to Dialog
  79.                                 alertDialog.setIcon(R.drawable.delete);
  80.  
  81.                                 // Setting Positive "Yes" Button
  82.                         alertDialog.setPositiveButton("YES",new
  83.                         DialogInterface.OnClickListener()
  84.                                 {
  85.                         public void onClick(DialogInterface dialog,int which)
  86.                         {
  87.                 // Write your code here to execute after dialog
  88.                                                                 Toast.makeText(getApplicationContext(), "You clicked on YES",
  89.       Toast.LENGTH_SHORT).show();
  90.         }
  91.         });
  92.         // Setting Negative "NO" Button
  93. alertDialog.setNegativeButton("NO",     new DialogInterface.OnClickListener()
  94. {
  95.  
  96.      public void onClick(DialogInterface dialog,int which)
  97.      {
  98.         // Write your code here to execute after dialog
  99.                                                                 Toast.makeText(getApplicationContext(), "You clicked on NO",
  100.  
  101.       Toast.LENGTH_SHORT).show();
  102.       dialog.cancel();
  103.     }
  104. });
  105. // Showing Alert Message
  106. alertDialog.show();
  107.  
  108.                         }
  109.                 });
  110.  
  111.                
  112.  
  113.  
  114.  
  115.  
  116. btnAlertThreeBtns.setOnClickListener(new View.OnClickListener()
  117. {
  118.  
  119.   public void onClick(View arg0)
  120.   {
  121.     // Creating alert Dialog with three Buttons
  122.  
  123. AlertDialog.Builder alertDialog = new AlertDialog.Builder(Dialog.this);
  124. // Setting Dialog Title
  125. alertDialog.setTitle("Save File...");
  126. // Setting Dialog Message
  127. alertDialog.setMessage("Do you want to save this file?");
  128.      // Setting Icon to Dialog
  129.      alertDialog.setIcon(R.drawable.save);
  130.      // Setting Positive Yes Button
  131. alertDialog.setPositiveButton("YES",new DialogInterface.OnClickListener()
  132. {
  133.  
  134.         public void onClick(DialogInterface dialog,int which)
  135.         {
  136.         // User pressed Cancel button. Write Logic Here                                 Toast.makeText(getApplicationContext(),"You clicked on YES",            Toast.LENGTH_SHORT).show();
  137.         }
  138.         });
  139.         // Setting Positive Yes Button
  140.         alertDialog.setNeutralButton("NO",new
  141.       DialogInterface.OnClickListener()
  142.         {
  143.  
  144.         public void onClick(DialogInterface dialog,int which)
  145.         {
  146.         Toast.makeText(getApplicationContext(),"You clicked on NO",
  147.       Toast.LENGTH_SHORT).show();
  148.         }
  149. });
  150. // Setting Positive "Cancel" Button
  151. alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener()
  152. {
  153.  
  154. public void onClick(DialogInterface dialog,int which)
  155. {
  156.                                                 Toast.makeText(getApplicationContext(),"You clicked on
  157.  
  158.      Cancel",Toast.LENGTH_SHORT).show();
  159. }
  160. });
  161.                                
  162. alertDialog.show();
  163.  
  164.                         }
  165.                 });
  166.         }
  167. }

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

mAN...where r yu located ? i can see we have something in common. perhaps we can come together and do something!? please see mi in facebook..karis.hacker(kariuki integer)

Hit mi in facebook karis.hacker is my username . Am interested in mobile app and iam want to launch a company for software development.

Add new comment