Nikesh Patel
Nikesh Patel

Reputation: 631

Controlling state of checkbox based on state of other checkboxes in android

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.

Upvotes: 1

Views: 152

Answers (4)

miva2
miva2

Reputation: 2151

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

Upvotes: 0

AlbAtNf
AlbAtNf

Reputation: 3909

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.

Upvotes: 1

Youssef Lahoud
Youssef Lahoud

Reputation: 529

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);
        }

Upvotes: 0

user5716019
user5716019

Reputation: 608

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
                }

Upvotes: 0

Related Questions