Android AlertDialog

Dialog is used for modal events which require users to take an action before they can proceed. The Android AlertDialog is the subclass of Dialog Class. The Dialog class is the base class of AlertDialog class. It is used to display the dialog message with 'Ok' and 'Cancel' buttons. The AlertDialog class is used for creating Alert notification or alert dialog box.

Definition of Dialog

“A dialog is a small window that prompts the user to make a decision or enter additional information.”

AlertDialog is composed of three regions:

1. Title defines the title of the AlertDialog.
2. Content area displays message or custom layout.
3. Action button is used to add one, two or three buttons on AlertDialog, but it does not allow more than three.

android dialog

To build an AlertDialog window you have to instantiate an AlertDialog.Builder with its constructor.

AlertDialog.Builder dialog = new AlertDialog.Builder(this);

AndroidDialog class has following three standard methods to add positive, negative and cancel button.

MethodsDescription
setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener)This method sets a listener and it will be invoked when the negative button of the dialog is pressed.
setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener)This method sets a listener and it will be invoked when the neutral button of the dialog is pressed.
setPositiveButton(CharSequence text, Dialognterface.OnClickListener listener)This method sets a listener and it will be invoked when the positive button of the dialog is pressed.

Example : AlertDialog

File Name: activity_main.xml

<LinearLayoutxmlns: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"
android:orientation = "vertical">

<Button
android:id="@+id/threeButtonDialog"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:layout_marginTop = "50dp"
android:text = "ThreeButtonDialog"
android:textSize = "30dp"/>
</LinearLayout>

File Name: MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{
     Button button1;
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
          super.onCreate (savedInstanceState);
          setContentView (R.layout.activity_main);
          button1 = (Button)findViewById(R.id.threeButtonDialog);
          button1.setOnClickListener (this);
     }
     @Override
     public void onClick(View v)
     {
          // TODO Auto-generated method stub
          showThreeButtonDialog();
     }
     public void showThreeButtonDialog()
     {
          AlertDialog.Builder dialog = newAlertDialog.Builder(this);
          dialog.setTitle("Save Confirm");
          dialog.setMessage("Are You Sure to save");
          dialog.setPositiveButton("OK",newDialogInterface.OnClickListener()
          {
               @Override
               publicvoidonClick(DialogInterface dialog, int which)
               {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "You clicked on OK", Toast.LENGTH_SHORT).show();
               }
          });
          dialog.setNegativeButton("NO", newDialogInterface.OnClickListener()
          {
               @Override
               publicvoidonClick(DialogInterface dialog, int which)
               {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
               }
          });
          dialog.setNeutralButton("Cancel", newDialogInterface.OnClickListener()
          {
               @Override
               public void onClick(DialogInterface dialog, int which)
               {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "You clicked on Cancel button", Toast.LENGTH_SHORT).show();
               }
          });
          AlertDialog ad = dialog.create();
          ad.show();
     }
}


Output:

alert dialog   alert dialog 1