glassraven glassraven - 26 days ago 6
Android Question

How to set the same property (invisibility) to several elements with a loop?

I have several elements that i want to set with the same property. Is it possible with a for loop? Thanks in advance. This is what I have so far:

public void invisible(){
int[] buttons = {R.id.key1, R.id.key2, R.id.key3, R.id.key4, R.id.key5, R.id.key6, R.id.key7
, R.id.key8, R.id.key9, R.id.key10, R.id.learn,R.id.kit1,R.id.kit2};
for(int i=0; i<buttons.length; i++){
//set invisible?
}
}

Answer
for (int i = 0; i < buttons.length; i++) {
   findViewById(buttons[i]).setVisibility(View.INVISIBLE);
}

This should work. You iterate over the view ids, search for the view in your layout hierachy and set the visibility.