How to add multiple buttons on a single AlertDialog
Asked Answered
F

6

51

I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this :enter image description here

Give Me a help :

I was using this.... to add multiple buttons

alertDialog.setButton(delete, "Delete", new OnClickListener() {
        
        public void onClick(View v) {
            // TODO Auto-generated method stub
            
        }
    });

but I found..., change setButton() to setButton2().. something like..... wt xcan i do for this....

Foray answered 3/9, 2012 at 8:23 Comment(1)
Make s Custom Dialog and inflated layout xml file to setVIew() it. Now add buttons as many as you want.Ten
H
24

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();
Hoarsen answered 3/9, 2012 at 8:25 Comment(0)
A
144

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();
Acescent answered 29/10, 2013 at 12:47 Comment(6)
This worked for me! However this gives you a vertical list rather than a horizontal.Behl
But how to add the message for the dialog content?Retrogressive
@teapeng : builder.setMessage() overrides the buttons, so I guess you're left with just builder.setTitle()Acescent
best ever, way to go!Franco
It crashes on builder.create().show();Vampirism
i got crash Resources$NotFoundException: Resource ID #0x0, that's really bad answerLoop
H
24

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();
Hoarsen answered 3/9, 2012 at 8:25 Comment(0)
C
3

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"

Capon answered 3/9, 2012 at 8:27 Comment(0)
B
0
Dialog dialog = new Dialog(context);
RelativeLayout featureLayout = (RelativeLayout) View.inflate(this, R.layout.yourview, null);
dialog.setContentView(featureLayout);
dialog.show();
Byers answered 3/9, 2012 at 8:29 Comment(0)
B
0
int item = 0;
ArrayList<String> list = new ArrayList<String>();
ArrayList<Integer> intList = new ArrayList<Integer>();
list.add("A");
list.add("B");
list.add("C");
list.add("D"); 
item = -1; 

 for (int i = 0; i < list.size(); i++) { 
    item++; 
    intList.add(i); 
  }

showRatingBarAlertDialog(intList);

private void showRatingBarAlertDialog(ArrayList<Integer> Id) {
    if (item != -1) {
        RatingFragment alertDialog = RatingFragment.instance(BaseActivity.this, Id.get(item), (ratingValue, comments) -> {
            CXLog.d(TAG, "select the rating::" + ratingValue);
            CXLog.d(TAG, "comment the feednback  " + comments);
            item--;
            showRatingBarAlertDialog(requestId);
        });
        alertDialog.show(CXBaseActivity.this.getFragmentManager(), "alertDialog");
    }
}
Baneful answered 22/7, 2016 at 6:27 Comment(0)
P
0

This is how I add any number of buttons (Kotlin code). In this example I add a button My Button.

val builder = AlertDialog.Builder(this)
                .setPositiveButton("OK", null)
                .setNegativeButton("Cancel", null)
val dialog = builder.create()
dialog.setOnShowListener {
        val anyButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
        class MyButton : MaterialButton (this, null, android.R.attr.buttonBarPositiveButtonStyle)
        val parentView = anyButton.parent as? ViewGroup
        val myButton = MyButton()
        myButton.text = "My Button"
        parentView?.addView(myButton)
}
dialog.show()
Palenque answered 10/10, 2023 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.