Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to create a Array of Elements in Android

I want to create a array of checkboxes so it can be easy to access them through a for loop. Something like this

for(int i = 0; i<10, i++)
{
    checkbox[i].visibility(view.GONE);
}

But I am not sure if there is a way to create a array like this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, it’s possible to create an array of Checkboxes. In below example i have created a fixed array of 10 Checkboxes.

// Create an array of CheckBoxes
CheckBox[] checkBoxes = new CheckBox[10];

// Initialize the CheckBoxes and add them to the parent layout
for (int i = 0; i < checkBoxes.length; i++) {
    checkBoxes[i] = new CheckBox(context);
    checkBoxes[i].setId(i);
    // Also add checkbox to parent layout
}

// Access the CheckBoxes using a for loop
for (int i = 0; i < checkBoxes.length; i++) {
    checkBoxes[i].setVisibility(View.GONE);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading