Jennifer
Jennifer

Reputation: 1852

How to check if more than 5 checkbox is checked in android?

As the title says, I want to check if more than 5 check box is checked in android. What I want to do is to check 5 checkbox and if I enter more or less than 5 checkbox it will show a alert . How will I do this ? I beleive that the pros here can help a noob like me. A example will be helpful for me to learn.

for(int i = 0; i< checkbox.length;i++){

            checkbox[i] = (CheckBox)findViewById(ids[i]);
            checkbox[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) { buttonView.setTextColor(Color.BLACK); 

                    buttonClicked.add(buttonView.getText().toString());
                                }
                    if (!isChecked) {buttonView.setTextColor(Color.GREEN); 

                        buttonClicked.remove(buttonView.getText().toString());

                    }
                }
            });

Upvotes: 0

Views: 418

Answers (1)

Remon Ramy
Remon Ramy

Reputation: 177

Set a counter that increases by 1 each time a checkbox is checked, and decreases by 1 each time a checkbox is unchecked. If the counter is > 5 then there are more than 5 checkboxes checked and you can display your alert.

Upvotes: 1

Related Questions