How to add multiple buttons on a single AlertDialog

You can only create a Alertdialog with 3 buttons if you dont make the view by yourself.

You can either make your own custom view in xml.

but i'd suggest you just make a List.

Check http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog "Adding a list"


I would inflate the AlertDialog with my own custom view (my_alert_dialog.xml).

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
//inflate view for alertdialog since we are using multiple views inside a viewgroup (root = Layout top-level) (linear, relative, framelayout etc..)
View view = inflater.inflate(R.layout.my_alert_dialog, (ViewGroup) findViewById(R.id.root)); 

Button button1 = (Button) view.findViewById(R.id.button1); // etc.. for button2,3,4.
alert.setView(view);
alert.show();

A simple solution 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", Toast.LENGTH_SHORT).show();
                        break;
                    case 1:
                        Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
builder.create().show();

Tags:

Android