Skip to content Skip to sidebar Skip to footer

How To Add Multiple Buttons On A Single Alertdialog

I have a butoon, on clicking of this button i want to open multiple buttons on a single AlertDialog like this : Give Me a help : I was using this.... to add multiple buttons alertD

Solution 1:

A simple solution without xml:

AlertDialog.Builderbuilder=newAlertDialog.Builder(context);
builder.setTitle("Title");
builder.setItems(newCharSequence[]
        {"button 1", "button 2", "button 3", "button 4"},
        newDialogInterface.OnClickListener() {
            publicvoidonClick(DialogInterface dialog, int which) {
                // The 'which' argument contains the index position// of the selected itemswitch (which) {
                    case0:
                        Toast.makeText(context, "clicked 1", Toast.LENGTH_SHORT).show();
                        break;
                    case1:
                        Toast.makeText(context, "clicked 2", Toast.LENGTH_SHORT).show();
                        break;
                    case2:
                        Toast.makeText(context, "clicked 3", Toast.LENGTH_SHORT).show();
                        break;
                    case3:
                        Toast.makeText(context, "clicked 4", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
builder.create().show();

Solution 2:

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

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

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

Solution 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"

Solution 4:

Dialogdialog=newDialog(context);
RelativeLayoutfeatureLayout= (RelativeLayout) View.inflate(this, R.layout.yourview, null);
dialog.setContentView(featureLayout);
dialog.show();

Solution 5:

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

privatevoidshowRatingBarAlertDialog(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");
    }
}

Post a Comment for "How To Add Multiple Buttons On A Single Alertdialog"