Skip to content Skip to sidebar Skip to footer

I Need To Uncheck A Checkbox When A Particular Checkbox Is Checked

I am a newbie... I have two checkboxes for example checkbox1 and checkbox2. I want checkbox2 to be unchecked when user checks checkbox1 and vice versa. plz help me how to code for

Solution 1:

You should use radio buttons for this behavior since they come with this functionality built in from the beginning.

A complete example can be found in the Hello Views Tutorial: Radio Button code

Solution 2:

Use RadioGroup (Container). Create the RadioGroup and drag the RadioButton to the RadioGroup. The uncheck/check action will happen automatically.

Solution 3:

I want to expand HenrikS' answer. If you still want to use checkboxes for this, you can set an onItemClickListener on both the checkboxes and need to unselect other in the onItemClick() Method. An example would be like this:-

CheckBox cb1,cb2;
//Considering you can initialize the above variables
cb1.setOnCheckedChangeListener(newOnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb2.setChecked(false);
    }
});
cb2.setOnCheckedChangeListener(newOnCheckedChangeListener{
    onCheckedChanged (CompoundButton view, boolean isChecked){
        cb1.setChecked(false);
    }
});

Due to this hassle, you should use radio buttons.

Post a Comment for "I Need To Uncheck A Checkbox When A Particular Checkbox Is Checked"