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 this functionality.
Asked
Active
Viewed 1,772 times
-1
-
Initially two checkboxes will be unchecked. When i select the first check box it gets checked and then when i select the second checkbox i should get checked and meanwhile first checkbox should be unchecked. Is it clear or you want me to explain still more? – Prakash Feb 16 '12 at 16:28
3 Answers
6
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

HenrikS
- 4,170
- 3
- 34
- 33
-
1Ya... u r rite... But i need it in checkbox format... creator gave the exact one i need.... – Prakash Feb 18 '12 at 05:33
-
2
Use RadioGroup (Container). Create the RadioGroup and drag the RadioButton to the RadioGroup. The uncheck/check action will happen automatically.

Guilherme
- 31
- 2
-
I was only using RadioButton and it did not work....but RadioGroup make it work! Thanks – Zohab Ali Mar 29 '17 at 12:00
1
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(new OnCheckedChangeListener{
onCheckedChanged (CompoundButton view, boolean isChecked){
cb2.setChecked(false);
}
});
cb2.setOnCheckedChangeListener(new OnCheckedChangeListener{
onCheckedChanged (CompoundButton view, boolean isChecked){
cb1.setChecked(false);
}
});
Due to this hassle, you should use radio buttons.

0xC0DED00D
- 19,522
- 20
- 117
- 184