AkEgo
AkEgo

Reputation: 23

Android Uncheck multiple Checkboxes

my Problem is that i want to create a Checklist with multiple Checkboxes. The biggest Problem is i have more than 100 Checkboxes. I would like an CLEAR Button that clears all Checkboxes by clicking.

How can I do that? And have you an example how to solve it?

The only way i know is that:

Button clear = (Button) findViewById(R.id.clearbtn);
    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox2);
            cb1.setChecked(false);
        }
    });

But that way isnt really effective with over 100 checkboxes ...

Upvotes: 1

Views: 1757

Answers (2)

AkEgo
AkEgo

Reputation: 23

I have solve it. Thanks to @Tuby and @android_hub for the idea with getChildCount().
And special thanks to @Hammad Akram. Now it works :D. My code now:

    final LinearLayout ll = (LinearLayout)findViewById(R.id.ll_a320_main);
    final int ccount = ll.getChildCount();

    Button clear = (Button)findViewById(R.id.btn_a320_clear);
    clear.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Toast.makeText(ACT_A320.this, "Child: "+ccount,Toast.LENGTH_LONG).show(); -- Test for checking count of Child
            for(int i=1; i<ccount;i++){
                v = ll.getChildAt(i);
                if(v instanceof CheckBox ){
                    ((CheckBox) v).setChecked(false);
                }
            }
        }
    });

Now all Checkboxes would detected and set to false.

Upvotes: 0

Hammad Akram
Hammad Akram

Reputation: 573

If you are keeping all the checkboxes on single ViewGroup then it can be done by getting all the childs of that ViewGroup and unchecking. For example 'parent' is the layout that contains all the checkboxes. You can uncheck all by:

 for (int i = 0; i < parent.getChildCount(); i++) {
        View view = parent.getChildAt(i);
        if (view instanceof CheckBox) {
            ((CheckBox) view).setChecked(false);
        }
    }

Upvotes: 1

Related Questions