AbAppletic AbAppletic - 4 months ago 13
Java Question

Set properties of multiple views at once

For example, if I have:

EditText A;
TextView B;
ImageView C;


And I want to set all their visibilities to
View.GONE
, how can I do it in a way that instead of this:

A.setVisibility(View.GONE);
B.setVisibility(View.GONE);
C.setVisibility(View.GONE);


I do this:

groupD.setVisibility(View.GONE);


without having to put all of them in one
RelativeLayout
and then setting the
RelativeLayout
to
View.GONE
? Is there a Java class for this? Or do I have to get a library for it?

If not, can I do it manually so I can organize my project which has 30 views? I don't want to set each group to be in its own
RelativeLayout
because all views are dependent on each other.

Answer

The best way will be passing the views to a single method. All of them are Views, so you can do something like this.

public void setViews(View view){
    view.setVisibility(View.GONE);
}

And call the method like this:

setViews(editText);