dimanche 6 septembre 2020

How to check a group of checkboxes by pressing a button in Android Studio?

I am trying to mark a checkbox group when clicking on a button. These checkboxes I have added dynamically.

Here I have some checkboxes (In this example there are only 4 checkboxes but there can be many more):

Checkbox checkboxes1;
Checkbox checkboxes2;
Checkbox checkboxes3;
Checkbox checkboxes4;

checkboxes1.setId(1);
checkboxes2.setId(2);
checkboxes3.setId(3);
checkboxes4.setId(4);

The problem is when I try to do checked all of them manually. I have this code:

public void onCheckboxAllChecked(View view){
Checkbox addAll;
for(int i = 0; i < 4; i++){
addAll = findViewById(i);
addAll.setChecked(true);
}
}

but when I try to press the button so that all the checkboxes are checked, the app stops and closes abruptly.

I put it in a for loop because there can be many more checkboxes (it will be the user who will define how many checkboxes are going to be added dynamically).

So, How to solve this problem? What am I doing wrong?

NOTE: The problem is not creating the checkboxes, because they have been created good, and when I check each of them individually, it works fine. The problem is checking all of them at the same time by clicking a button.




Aucun commentaire:

Enregistrer un commentaire