Susheel
Susheel

Reputation: 784

Set checkboxes status in Multichoice AlertDialog in Android

Following is my code for my AlertDialog.Builder

    final String[] values = new String[] {"Select All", "Android", "ios", "windows", "Blackberry"};
    final ArrayList<String> selecteditems = new ArrayList<String>();
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Select your favorite OS");
      builder.setMultiChoiceItems(values, null , new DialogInterface.OnMultiChoiceClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which, boolean isChecked) {

            if (isChecked) {
                selecteditems.add(String.valueOf(which));

            } else {

                selecteditems.remove(Integer.valueOf(which));
            }
        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();

        }
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
           dialog.dismiss();

        }
    });

    AlertDialog dialog = builder.create();
    dialog.show();

What I want to do is to be able to detect when 'Select All' is checked, which I can from the isChecked loop. But I want to be able to set all the options to checked when the user checks 'Select All' and remove the checks when user unchecks 'Select All'.

Upvotes: 5

Views: 4117

Answers (1)

tasomaniac
tasomaniac

Reputation: 10342

It should be something like the following code. The key point is to provide a boolean[] array of checkedItems and update it later when you try to select all.

  • checkedItems array should be updated (because the Dialog still has reference to it.)
  • And dialog.getListView().setItemChecked(i, true); should be called for every item.

    items.add(0, "Select All");
    
    boolean[] checkedItems = new boolean[items.size()];
    
    AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setTitle(dialogTitle)
            .setMultiChoiceItems(items.toArray(new String[items.size()]), checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    
                    if(which == 0) {
                        if(isChecked) {
                            multichoiceDialog.getListView().setItemChecked(0, true);
                            checkedItems[0] = true;
                            for(int i=1; i< checkedItems.length; i++) {
                                checkedItems[i] = true;
                                multichoiceDialog.getListView().setItemChecked(i, true);
                            }
    
                        }
                    }
                }
            })
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
    
                @Override
                public void onClick(DialogInterface dialog, int which) {
    
                }
            });
    
    multichoiceDialog = builder.create();
    multichoiceDialog.show();
    

Upvotes: 3

Related Questions