Android AlertDialog with 3 buttons example


Posted on : Mon, 20th Apr 15 10:09 am (Updated on : Mon, 20th Apr 15 10:09 am) UTC
Author : Code2care
100+ votes


Figure : Android AlertDialog Example

You might have made use of Toast to display messages that fades in stays for few seconds and fades away. If you want to display some message and let user select the response (Yes or No) then we can make use of Dialog called as AlertDialog.

AlertDialog is a subclass of Dialog from android.app package. You can create a dialog with one, two or even three buttons. It is also possible to display only text message using setMessage() method.

There are three functions for adding Buttons to Android Dialog,

  1. setPositiveButton(int textId, DialogInterface.OnClickListener listener) :

    This is Yes button, when clicked the code written in the OnClickListener onClick() method will be displayed.

  2. setNegativeButton(int textId, DialogInterface.OnClickListener listener) :

    This is just like the setPositiveButton method which acts a "NO" negative button, we will write the logic in OnClickListener anonymous class.

  3. setNeutralButton(int textId, DialogInterface.OnClickListener listener) :

    This is how we can set the 3rd button. It is a called the Neutral button.

    Android AlertDialog Snippet Language : Java
     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    
    
            builder.setTitle("AlertDialog Example");
    
    
            builder.setMessage("This is an Example of Android AlertDialog with 3 Buttons!!");
    
    
            //Button One : Yes
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "Yes button Clicked!", Toast.LENGTH_LONG).show();
                }
            });
    
    
            //Button Two : No
            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "No button Clicked!", Toast.LENGTH_LONG).show();
                    dialog.cancel();
                }
            });
    
    
            //Button Three : Neutral
            builder.setNeutralButton("Can't Say!", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this, "Neutral button Clicked!", Toast.LENGTH_LONG).show();
                    dialog.cancel();
                }
            });
    
    
            AlertDialog diag = builder.create();
            diag.show();

    Code2care's mission is to share varied knowledge in technical and non-technical areas gathered during day-to-day learnings and development activities so that our visitors can leverage this portal to find solutions to their queries without re-inventing the wheel. Technical posts include Learnings, Tutorials, Video Tutorials, Code Snippets, Tips-n-tricks.

    Follow us : Facebook - Twitter - Google +