Question

I am working on an android project with many activities and classes.

In few of the Layouts, I have a facility in which a user can select mark all so that all the checkboxes get selected.

But, the issue I am facing is I know only the one way (which is too lengthy) to do this - By creating a markall layout for each of those existing layouts, pasting the original layout code in the markall layout code and then giving all checkboxes as checked. This requires me to create xmls for all unmarkings also for each layout.

Please tell me a way in which I can create one xml for markall which has all checked checkboxes, and then call that layout on top of original layout when mark all is clicked.

Was it helpful?

Solution

Lets assume you have following Layout

<LinearLayout
   android:id="@+id/checkbox_container"
   ...params>
   <CheckBox ...params/>
   <TextView ...params/>
   <CheckBox ...params/>
   <SomeView ...params/>
   <CheckBox ...params/>
</LinearLayout>

get your checkBoxLayout like this inside Activity

LinearLayout container = (LinearLayout)findViewById(R.id.checkbox_container);

Iterate over the childs inside the container like this:

for (int i = 0; i < container.getChildCount(); i++){
    View v = container.getChildAt(i);
    if (v instanceof CheckBox){
        CheckBox cb = (CheckBox)v;
        // isChecked should be a boolean indicating if every Checkbox should be checked or not
        cb.setChecked(isChecked)
    }
}

OTHER TIPS

CheckBox MarkAllCheckBox =
    (CheckBox) findViewById( R.id.MarkAllCheckBox);
MarkAllCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
            checkBox1.setChecked("true");
            checkBox2.setChecked("true");
            checkBox3.setChecked("true");
            //so on
        }
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top