Search code examples
androidandroid-checkbox

how to limit number of checked checkBoxes android


how can I limit the number of checked checkboxes in android? I have multiple checkboxes being added programatically and it's difficult to keep track of them.

here's the code used to add them:

                        final CheckBox currentVariantCheckbox = new CheckBox(getApplicationContext());
                        checkBoxGroupList.add(currentVariantCheckbox);
                        Log.d(TAG, "onDataChange: added " + currentVariantCheckbox + " to the checkboxgrouplist; size = " + checkBoxGroupList.size());
                        currentVariantCheckbox.setChecked((Boolean) currentVariant.child("checked").getValue());
                        LinearLayout checkboxGroupLayout = new LinearLayout(getApplicationContext());
                        checkboxGroupLayout.setOrientation(LinearLayout.HORIZONTAL);

                        currentVariantCheckbox.setText(currentVariant.child("name").getValue(String.class));
                        TextView currentVariantPriceTag = new TextView(getApplicationContext());
                        checkboxGroupLayout.addView(currentVariantCheckbox);
                        if (currentVariant.child("price").exists()) {
                            currentVariantPriceTag.setText("+" + currentVariant.child("price").getValue(float.class).toString() + " €");
                            checkboxGroupLayout.addView(currentVariantPriceTag);

Solution

  • ok so instead of using a onCheckedStateChangedLister I used OnClickListener. And I created an ArrayList to keep track of all the checked checkboxes:

    final ArrayList<CheckBox> checkedList = new ArrayList<>();//this is the list to keep track of checked checkboxes
    int maxOptions = 3
    int minOptions = 1
    currentVariantCheckbox.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Boolean currentCheckState = currentVariantCheckbox.isChecked();
                                    if (currentCheckState) {//if the clicked checkboxes was unchecked and is now checked
                                        checkedList.add(currentVariantCheckbox);
                                        Log.d(TAG, "onClick: added " + currentVariantCheckbox + " ;checkedList.size is now: " + checkedList.size());
                                        if (checkedList.size() >= maxOptions) {
                                            checkedList.get(0).setChecked(false);
                                            checkedList.remove(0);// if limit exceeded remove the first element from the checked list
                                            Log.d(TAG, "onCheckedChanged: checkedList's size is now: " + checkedList.size());
                                        }
                                    } else if (checkedList.size() <= minOptions) {
                                        currentVariantCheckbox.setChecked(true);
    // if the list is empty just add the clicked checkbox to the list for example here 0
    // and it will be checked automatically
                                    } else {
                                        checkedList.remove(currentVariantCheckbox);
    // if the checkbox was already checked and no limit is exceeded
    // then it will be unchecked therfore it should be removed from checkedList
                                    }
                                }
                            });