Code example for AlertDialog.Builder

Methods: setMultiChoiceItems

0
        if (mEntries.length != mEntryKeys.length || mEntryKeys.length != mInitialValues.length) {
            throw new IllegalStateException(
                    "MultiChoicePreference entries, entryKeys, and initialValues arrays must be the same length"); 
        } 
 
        builder.setMultiChoiceItems(mEntries, mValues, new DialogInterface.OnMultiChoiceClickListener() {
            @Override 
            public void onClick(DialogInterface dialog, int which, boolean val) {
                // mValues is automatically updated when checkboxes are clicked 
 
                // enable positive button only if at least one item is checked 
                boolean enabled = false;
                for (int i = 0; i < mValues.length; i++) {
                    if (mValues[i]) {
                        enabled = true;
                        break; 
                    } 
                } 
                Button button = ((AlertDialog) dialog).getButton(DialogInterface.BUTTON_POSITIVE);
                if (button.isEnabled() != enabled)
                    button.setEnabled(enabled);