check if all check boxes are checked. android

mohand abdelazez :

I'm trying to handle an event where if the person checks all the checkboxes an action should happen, which is to make a button clickable.

so far I could only check if a box is checked or unchecked but can't find a way to set a counter for how many checkboxes clicked.

The checkboxes are going to be dynamic but in this code sample, I just used static values.

I tried to set a counter and if I click a checkbox it should increment but couldn't figure out the right way.

// The method I call

  ArrayList<String> arrayList= new ArrayList<>();
            arrayList.add("2Pac");
            arrayList.add("Biggie");
            arrayList.add("Nas");
            // Create Checkbox Dynamically
            for(int i=0;i<arrayList.size();i++) {

                CheckBox checkBox = new CheckBox(getContext());
                checkBox.setText(arrayList.get(i));
                checkBox.setTextSize(20);
                checkBox.setId(++checkBoxID);
                checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
                checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                   // int counter = -1;
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {
                       // String msg = "You have " + (isChecked ? "checked" : "unchecked") + " this box it Checkbox.";
                        //Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                        if(checkBox.isChecked())
                        {
                            //Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
                        }else
                            {
                          //      counter++;
                             //   Toast.makeText(getActivity(), ""+ counter, Toast.LENGTH_SHORT).show();
                            }
                    }
                });

                for(int j=0;j<allCheckBox.size();j++)
                {
                    if(allCheckBox.size()==arrayList.size())
                    {
                        //Toast.makeText(MainActivity.this, ""+ checkBox.getId(), Toast.LENGTH_SHORT).show();
                    }
                }
                // Add Checkbox to LinearLayout
                if (linearLayout != null) {
                    linearLayout.addView(checkBox);
                }

I've spent over a day trying to figure this one so far !! any help or idea is really appreciated.

Zakaria M. Jawas :

Define the counter outside the for loop with initial value 0 and when the user checks a checkbox increase it by one and when the user unchecks a checkbox decrease the counter by one and in the same onCheckedChanged listener check for the number of checkboxes like this

 ArrayList<String> arrayList= new ArrayList<>();
 arrayList.add("2Pac");
 arrayList.add("Biggie");
 arrayList.add("Nas");

 int counter = 0; //all checkboxes unchecked
 // Create Checkbox Dynamically
 for(int i=0;i<arrayList.size();i++) {

     CheckBox checkBox = new CheckBox(getContext());
     checkBox.setText(arrayList.get(i));
     checkBox.setTextSize(20);
     checkBox.setId(++checkBoxID);
     checkBox.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

     checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
                    {

                        if(checkBox.isChecked())
                        {
                           counter++;
                           if (counter == arrayList.size()) {
                              //make your button clickable here
                           }
                        } else {
                           counter--;
                           //disable your button incase the user uncheck one of the checkboxes after all checkboxes were checked
                        } //end if
                    }
     });


     // Add Checkbox to LinearLayout
     if (linearLayout != null) {
         linearLayout.addView(checkBox);
     } //end if
 } //end for

I wrote the code from my phone so i didn't test the code I hope it works

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27139&siteId=1