I have three checkboxes and if any two of them are selected I want to disable the third checkbox immediately. I am not getting how to proceed.
-
I have references to three checkboxes but I am not getting any logic to proceed forward. I atleast need logic to move forward – Nikesh Patel Feb 05 '16 at 08:45
-
look at this: http://stackoverflow.com/questions/8386832/android-checkbox-listener – Pooya Feb 05 '16 at 08:50
-
I think it's strange that this question got at least two upvotes. Downvote from here. As stated above, you should show what you have done so far and narrow down the problem instead of asking for a full-blown solution. Also describe what resources you have read (developer docs, tutorials etc.) in order to get an understanding of the problem at hand. We are here to help, but we are not going to do all your work for you. Show some commitment, please. – Janus Varmarken Feb 05 '16 at 10:21
-
@ janus as I have stated earlier I just wanted the logic to proceed forward. And if you really want to know what I have read and what code I have written, don't worry as soon as I get the required result using the logic provided by my fellow mates I will post it here – Nikesh Patel Feb 05 '16 at 15:25
4 Answers
Manage your CheckBoxes in a List, so you can iterate over them.
List<CheckBox> boxes = new ArrayList<>();
Assign them like so (in onCreate()
of your activity)
boxes.add((CheckBox) findViewById(R.id.checkbox1));
boxes.add((CheckBox) findViewById(R.id.checkbox2));
boxes.add((CheckBox) findViewById(R.id.checkbox3));
Add an onCheckedChangeListener
like this:
for(CheckBox box : boxes){
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
manageBoxes();
}
});
}
and finally your manageBoxes()
private void manageBoxes(){
int count = 0;
for(CheckBox box : boxes){
count += box.isChecked()?1:0;
}
if(count>=2){
for(CheckBox box : boxes){
box.setEnabled(box.isChecked());
box.setClickable(box.isChecked()); // not sure if needed
}
}else{
// reset all boxes
for(CheckBox box : boxes){
box.setEnabled(true);
box.setClickable(true);
}
}
}
Just a quick and dirty thought. Hope this works. Plus: This is scalable, so you could include some more checkboxes if needed.

- 3,859
- 3
- 25
- 29
Add a change handler or a click handler on each of the checkboxes and let it call a small method that is responsible for checking the value of each checkbox: if two of them are true disable the third one.
For example:
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
handleCheckBoxes();
}
});
//Do this for all the checkboxes
private void handleCheckBoxes(){
int checkedCount = 0;
CheckBox uncheckedBox = null;
if(checkBox1.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox1;
}
if(checkBox2.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox2;
}
if(checkBox3.isChecked()){
checkedCount++;
}
else{
uncheckedBox = checkBox3;
}
if(checkedCount == 2){
uncheckedBox .setEnabled(false);
}
else{
//enable all checkboxes
checkBox1.setEnables(true);
checkBox2.setEnables(true);
checkBox3.setEnables(true);
}

- 529
- 3
- 8
-
-
yes for example I want to calculate voltage then I need any of the two quantities from current resistance or power. So if user selects current and resistance checkboxes then the power checkbox should get disabled – Nikesh Patel Feb 05 '16 at 08:53
-
@NikeshPatel Please check the code in the edited post. I did not test it but you can see the logic... hope this helps – Youssef Lahoud Feb 05 '16 at 09:03
-
-
@miva2 this is not a code to deploy man. He asked for logic this is just to show the procedure. – Youssef Lahoud Feb 05 '16 at 10:20
-
@miva2 oh i just checked your answer, you're right! It's much cleaner that way... – Youssef Lahoud Feb 05 '16 at 10:26
-
@Youssef I am working on the the code provided by you. Will get to you soon – Nikesh Patel Feb 05 '16 at 15:19
Try to implement this way :
private ArrayList<Integer> integers = null;
integers = new ArrayList<>();
chkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
int position = (int) yourview.getTag();
if (chkBox.isChecked()) {
integers.add(position);
chkBox.setChecked(true);
} else {
if (integers.size() > 0) {
for (int i=0; i<integers.size(); i++) {
if (integers.get(i) == position) {
integers.remove(i);
}
}
});
if (integers.size() > 2) {
//Disable your third checkbox
}

- 598
- 4
- 17
In pseudo code this would be:
Create variable to hold the number of checkboxes checked, let's call it amountChecked
When a checkbox gets checked => increment the amountChecked
When a checkbox gets checked => check if the amountChecked
is equal or greater than the allowedAmountChecked
(in your case this is 2)
=> if it is greater or equal to the allowedAmountChecked
then disable the remaining checkboxes

- 2,111
- 25
- 34
-
But with a counter like that, you still have to take care of display rotations. storing the counter in savedInstanceState eg. – AlbAtNf Feb 05 '16 at 10:29