How can I add a third button to an Android Alert Dialog?

AndroidAndroid AlertdialogAndroid Dialog

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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();

Solution 2 - Android

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) {

        //...

    }});

Solution 3 - Android

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();

Solution 4 - Android

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();

Solution 5 - Android

With Jetpack compose 1.0.x 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 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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser180825View Question on Stackoverflow
Solution 1 - AndroidninjasenseView Answer on Stackoverflow
Solution 2 - AndroidsahhhmView Answer on Stackoverflow
Solution 3 - AndroidOded BreinerView Answer on Stackoverflow
Solution 4 - AndroidNirav BhavsarView Answer on Stackoverflow
Solution 5 - AndroidGabriele MariottiView Answer on Stackoverflow