Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

like image 748
Susheel Avatar asked Oct 20 '22 12:10

Susheel


1 Answers

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();
    
like image 61
tasomaniac Avatar answered Oct 22 '22 02:10

tasomaniac