Manju
Manju

Reputation: 65

Simple code to check many checkbox android

I have 20 checkbox and want to check is all checkbox is checked. I use the below case to check all using OR operator.

if (((CheckBox) v).isChecked()) {
                Toast.makeText(MyAndroidAppActivity.this,
                   "Bro, try Android :)", Toast.LENGTH_LONG).show();
            }

But is there any optimized way to check whether all Checkbox is selected

Upvotes: 0

Views: 172

Answers (1)

Kiran Kumar
Kiran Kumar

Reputation: 1212

An optimised way :

  • Maintain an int field in your activity which has a default value of 0.
  • In your checkbox's onChecked listener, increment or decrement the value of the int field by 1 depending on whether it is checked or unchecked.
  • If the value of the int field is equal to 20, that means all of the checkboxes are checked.

Alternatively, you could also run a loop with the same code you mentioned as the body.

Upvotes: 1

Related Questions