Code Droid
Code Droid

Reputation: 10472

Hiding/Unhiding Inner Layouts in Android

I need to have the capacity to hide/unhide (in the sense of View.GONE) an entire linear layout row in an Activity. Is this the best approach to just to get a reference to the inner layout that is part of a greater relative layout and set visibility on that inner layout to gone? Also should I maintain a reference to the layout so I can just do innerLayout.setVisibility(View.GONE). If so what type of reference should it be ? final or is it better to just do a find each time I want to hide/unhide. Somehow keeping a direct reference to a layout does not seem quite right. On the other hand, I don't want to set each item in the layout to be gone or put a findBy to locate it each time I hide/unhide.

Perhaps add/remove layout is better form? but then I would need to add to the right place in the View hierarchy, putting this logic in code is also not a good idea.

Upvotes: 3

Views: 3686

Answers (1)

FabianCook
FabianCook

Reputation: 20557

Is yur linear layout set in XML or made programmatically?

if its done in XML:

note that if any views use this view as a reference in the layout such as android:layout_below="@+id/this" then that won't be a very good idea.

You can use something like this, it will hide all of the child views and itself, not hide but completely gone (No space taken up by it)

Use this as a reference either in the class if you want to use it in multiple methods or in the method which you are using it

View layout;

Then in onCreate call this

layout = findViewById(R.id.linearLayout);

then to make it dissapear:

layout.setVisibility(View.GONE);

or

layout.setVisibility(8);

and to get it back

layout.setVisibility(View.VISIBLE);

or

layout.setVisibility(0);

Upvotes: 2

Related Questions