Akeshwar Jha
Akeshwar Jha

Reputation: 4576

How to make a set of views invisible in android

I'm trying to make a set of views (that include several textviews and buttons - all in different parent layouts, but in the same activity) invisible if a particular condition is evaluated to false.

The conventional way to do that would be:

findViewById(R.id.myview).setVisibility(View.INVISIBLE);

My question is, will I have to do this for all the views one by one that I want to be invisible, And then toggle them back when I want them visible? Or, is there a way to avoid the code-repetition?

Upvotes: 2

Views: 3574

Answers (3)

Hemant Parmar
Hemant Parmar

Reputation: 3976

Use varargs method for show or hide multiple views.

for example if you have views like view1, view2.....etc

then just call setVisibility(View.VISIBLE,view1,view2)

public static void setVisibility(int visibility, View... views){
    for (View view : views){
       view.setVisibility(visibility);
    }
}

Upvotes: 0

br00
br00

Reputation: 1454

you can use something like this. It's not the most elegant solution but works.

The idea is give to each view that you want to hide a same content description, because in the same layout you can not use same id for multiple view. With the same content description you can find all views in your layout and hide them. That's an example considering the first layout as Linear. You can change obviously ;)

public class TestActivity extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_test);

            LinearLayout rootLayout = (LinearLayout)findViewById(R.id.rootLayout);

            int childcount = rootLayout.getChildCount();
            for (int i=0; i < childcount; i++){
                View v = rootLayout.getChildAt(i);
                if(v.getContentDescription() != null && v.getContentDescription().equals("invisibleView")){
                    v.setVisibility(View.INVISIBLE);
                   //I suggest you to use GONE instead of INVISIBLE to remove the space of the view
                }
            }

        }
}

in your xml give to the object that you want to hide this property

android:contentDescription="invisibleView"

Upvotes: 1

antonio
antonio

Reputation: 18262

If the Views are in different parents , you can't do it directly, but you can implement a method to change the visibility of a bunch of Views if you want to keep your code clean:

List<View> relatedViews = new ArrayList<>();

// ...

relatedViews.add(view1);
relatedViews.add(view2);
relatedViews.add(view3);

// ...

changeVisibility(relatedViews, View.INVISIBLE);

// ...

private void changeVisibility(List<View> views, int visibility) {
    for (View view : views) {
        view.setVisibility(visibility);
    }
}

As a side note, you may want to change the visibility to View.GONE instead of View.INVISIBLE so it doesn't take any space in the layout.

Upvotes: 4

Related Questions