128

The API says that the Alert Dialog can have one, two or three buttons, but the SDK only allows for a positive and negative button. How then can I add a third button?

Ashok Kumar
  • 131
  • 1
  • 12

5 Answers5

245

When you are creating the dialog, add something like this to the builder:

builder = new AlertDialog.Builder(context);
builder.setTitle("Test");
builder.setIcon(R.drawable.icon);
builder.setMessage("test");
builder.setPositiveButton("Call Now",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });

builder.setNeutralButton("Setup",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                context.startActivity(new Intent(context, Setup.class));
                //dialog.cancel();
            }
        });

builder.setNegativeButton("Exit",
        new DialogInterface.OnClickListener()
        {
            public void onClick(DialogInterface dialog, int id)
            {
                dialog.cancel();
            }
        });
builder.create().show();
Devendra Singh
  • 2,343
  • 4
  • 26
  • 47
ninjasense
  • 13,756
  • 19
  • 75
  • 92
144

This code snippet should help explain the three different buttons you can use:

    alertDialog = new AlertDialog.Builder(this).create();

    alertDialog.setTitle("Dialog Button");

    alertDialog.setMessage("This is a three-button dialog!");

    alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Button 1 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    } }); 

    alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Button 2 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    }}); 

    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "Button 3 Text", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface dialog, int id) {

        //...

    }});
jvperrin
  • 3,368
  • 1
  • 23
  • 33
sahhhm
  • 5,325
  • 2
  • 27
  • 22
50

Add any number of buttons without xml:

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle("Title");
    builder.setItems(new CharSequence[]
            {"button 1", "button 2", "button 3", "button 4"},
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    // The 'which' argument contains the index position
                    // of the selected item
                    switch (which) {
                        case 0:
                            Toast.makeText(context, "clicked 1", 0).show();
                            break;
                        case 1:
                            Toast.makeText(context, "clicked 2", 0).show();
                            break;
                        case 2:
                            Toast.makeText(context, "clicked 3", 0).show();
                            break;
                        case 3:
                            Toast.makeText(context, "clicked 4", 0).show();
                            break;
                    }
                }
            });
    builder.create().show();
Oded Breiner
  • 28,523
  • 10
  • 105
  • 71
  • 3
    This solution is not adding buttons, e.g. "OK", "Cancel", etc., but rather setting the items to be displayed in the list of choices, e.g., "Option 1", "Option 2", etc.. Since the question mentions that "the SDK only allows for a positive and negative button", this doesn't answer how to overcome that limitation. – d60402 Mar 25 '15 at 15:09
  • 3
    @d60402 - you have a good point, but if a developer looking for a way to create a dialog with more than 2 buttons comes across this answer... Having this option is very useful because it's more flexible with the number of buttons. To put it differently, if positive and negative buttons are not enough for what you're doing, 3 buttons might become 4 pretty quickly. Hence the votes. – Oded Breiner Mar 25 '15 at 15:35
  • @d60402 The title of the question is how to add a third button, not how to show neutral button. – Boris Treukhov Aug 26 '15 at 10:27
  • @Boris Treukhov - If the dialog has a positive button and a negative button, that means the dialog has two buttons. Adding the neutral button would give the dialog the third button. This is essentially what the top two answers to this question are doing. – d60402 Aug 26 '15 at 13:38
  • As shown in http://stackoverflow.com/a/19658646/2914140, this solution will show buttons when there is no dialog message set. – CoolMind Feb 24 '16 at 13:47
  • Great solution. Helps a lot when having a requirement of using more than 3 buttons in the alert dialog! – vss Nov 21 '17 at 09:57
  • the solution is good but when I add message, it hides the buttons – Shahid Ghafoor May 23 '18 at 10:36
  • This approach also doesn't allow any body text for the dialog, and if you need more than 3 action buttons in a dialog, that's probably just creating a bad UX and you should avoid it. Another thing is that having buttons instead of list items has benefits too, like out-of-the-box accessibility, consistent theming across the app and OS, and proper separation of positive/neutral and negative choices -- so I believe that the API is built like this for a reason (strangely enough). – milosmns Jul 24 '19 at 09:20
7

With Jetpack compose M2 you can use the AlertDialog with the buttons parameter:

val openDialog = remember { mutableStateOf(true) }

if (openDialog.value) {
    AlertDialog(
        onDismissRequest = {
            openDialog.value = false
        },
        title = {
            Text(text = "Title")
        },
        text = {
            Text(
                "Message area"
            )
        },
        buttons = {
            Row(
                modifier = Modifier.padding(all = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(onClick = {  }){
                    Text("First")
                }
                TextButton(onClick = { }) {
                    Text("Second")
                }
                TextButton(onClick = {  }) {
                    Text("Third")
                }
            }
        }
    )
}

enter image description here

With Compose M3 you can use the confirmButton attribute:

  //androidx.compose.material3
  AlertDialog( 
        //..
        confirmButton = {
            Row(
                modifier = Modifier.padding(all = 8.dp),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                TextButton(onClick = {  }){
                    Text("First")
                }
                TextButton(onClick = { }) {
                    Text("Second")
                }
                TextButton(onClick = {  }) {
                    Text("Third")
                }
            }
        }
    )

With the Material Components library you can use:

MaterialAlertDialogBuilder(context)
        .setTitle(resources.getString(R.string.title))
        .setMessage(resources.getString(R.string.supporting_text))
        .setNeutralButton(resources.getString(R.string.cancel)) { dialog, which ->
            // Respond to neutral button press
        }
        .setNegativeButton(resources.getString(R.string.decline)) { dialog, which ->
            // Respond to negative button press
        }
        .setPositiveButton(resources.getString(R.string.accept)) { dialog, which ->
            // Respond to positive button press
        }
        .show()

enter image description here

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • With JetpackCompose, we don't have buttons with AlertDialog anymore..any idea how to achieve multiple buttons with later versions. (I am using version 1.3.0) – sgupta Aug 31 '22 at 21:24
  • 1
    @sgupta with Compose M3 (material3) you can use the confirmButton attribute. Answer updated. – Gabriele Mariotti Sep 01 '22 at 11:51
5
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    this);

            // set title
            alertDialogBuilder.setTitle("To Do List");

            // set dialog message
            alertDialogBuilder
                    .setMessage("What do you want?")
                    .setCancelable(false)
                    .setPositiveButton("Delete All", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // if this button is clicked, close
                            // current activity




                            dialog.cancel();


                        }
                    }).setNeutralButton("Delete", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // if this button is clicked, close
                    // current activity




                    dialog.cancel();

                }
            })
                    .setNegativeButton("Cancel", 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();
Pang
  • 9,564
  • 146
  • 81
  • 122
Nirav Bhavsar
  • 2,133
  • 2
  • 20
  • 24