Jim
Jim

Reputation: 9234

Android : How to disable CheckBox in AlertDialog?

Ok guys...I need to create a Alert dialog with 3 check boxes. If the top check box is clicked, 2 another one should be clicked and disabled !! I do them clicked, but not disabled. And i have no idea how to do that.

@Override 
protected Dialog onCreateDialog (int id) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);        
builder.setTitle("AA");             
builder.setMultiChoiceItems(mStrings, mCheckedItems, new DialogInterface.OnMultiChoiceClickListener() {                     
public void onClick(final DialogInterface dialog, int which, boolean isChecked) {


                        switch (which) {
                        case 0: {

                        if(isChecked==true)  {
                            for (int i = 1; i<=2; i++) {                                    
                            ((AlertDialog) dialog).getListView().setItemChecked(i, true);                           
                             }
                        }

                        if (isChecked==false) {
                            for (int i = 1; i<=2; i++) {                                    
                                ((AlertDialog) dialog).getListView().setItemChecked(i, false);                          
                                 }

                             break;
                        }

And this solution is not good to. Some times its not click all checkboxes. Have anybody any idea ?

Upvotes: 0

Views: 6320

Answers (3)

Sumith.Pattar
Sumith.Pattar

Reputation: 100

/* Please set appropriate boolean value in the boolean array which you have
passed as paramater for
 builder.setMultiChoiceItems(StringArray,BooleanArray, Listener)
in order to check or uncheck items in dialog */

@Override 
protected Dialog onCreateDialog (int id) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("AA"); 

builder.setMultiChoiceItems(mStrings, mCheckedItems,
        DialogInterface.OnMultiChoiceClickListener() { 
    public void onClick(final DialogInterface dialog, int which, boolean isChecked) { 

        switch (which) {
            case 0: {
                if(isChecked)  {
                    for (int i = 1; i<=2; i++) {
                        mCheckedItems[i] =false;
                    }
                } else {
                    for (int i = 1; i<=2; i++) {
                        ((AlertDialog) dialog).getListView().setItemChecked(i,false);
                        mCheckedItems[i] =false;
                    }
                }
                break;
        }

Upvotes: 1

Girish
Girish

Reputation: 2416

make mCheckeditems[i]=false if u want the checkbox unchecked or vice versa

Upvotes: 0

FoamyGuy
FoamyGuy

Reputation: 46856

You should be able to call .setEnabled(false) on the two checkboxes you want to disable in your onClick() listener. Out of curiosity why are you using a for loop structure to loop thru 2 items and set them to checked. It seems to me that calling .setChecked() on both of the in 2 successive calls would simplify this proccess.

code sample:

//This line has to go after your dialog.show(); call
    CheckBox chkBox = (CheckBox) dialog.findViewById(R.id.yourCheckBox);
//This line will go in your OnClickListener.
    chkBox.setEnabled(false);

Upvotes: 3

Related Questions