Angelo DeMichaels
Angelo DeMichaels

Reputation: 251

Check/Uncheck multiple checkboxes inside TableLayout

I have a TableLayout; first column of the first TableRow is a CheckBox. The TableLayout and the first TableRow are created in the .axml file. I create and populate the rest of the TableRows in the .cs file. The first column of each programmatically created TableRow is also a CheckBox.

Using the CheckBox in the first TableRow I would like to Check/Uncheck all programmatically created checkboxes.

In a Windows Forms application I would do this by gathering the checkboxes into an array and iterating through the array to set the checked property on each. In my Android app I can't figure out how to do this.

Upvotes: 2

Views: 770

Answers (1)

William Barbosa
William Barbosa

Reputation: 4995

You need to iterate over all TableRows inside your TableLayout, find your checkboxes and checking/unchecking them. To do so, add a listener to the click event of your first checkbox that does something like this:

//Number of TableRows
var count = tableLayout.ChildCount;
//Position of you checkbox inside each TableRow
//TODO: make this a constant outside the listener method
var checkBoxPosition = 0;

for (int i = 0; i < count ; i++) {
    var tableRow = tableLayout.GetChildAt(i) as TableRow;
    if (tableRow != null) {
        var checkBox = tableRow .GetChildAt(checkBoxPosition) as CheckBox;
         if (checkBox != null) {
             //TODO: Use state of the main checkbox here
             checkBox.Checked = true;
         } 
    }
}

Upvotes: 1

Related Questions