how to hide linearlayout from java code?

Android

Android Problem Overview


I want to hide the linear layout so i used

LinearLayout mainLayout=(LinearLayout)this.findViewById(R.id.mainLayout);
mainLayout.setVisibility(2);

but why is doesn't hide ??!!!

Android Solutions


Solution 1 - Android

Use:

mainLayout.setVisibility(LinearLayout.GONE);

Solution 2 - Android

You can also set the visibility in your layout.xml if you want it hidden when your application first starts. android:visibility="gone" should do the trick. This way it is hidden from the very start when the layout is initialized by your app.

Solution 3 - Android

Also you can use LinearLayout.INVISIBLE.

The difference is (Android Documentation):

> View.GONE - This view is invisible, and it doesn't take any space for > layout purposes. > > View.INVISIBLE This view is invisible, but it still takes up space for > layout purposes.

You can choose anyone based upon your design.

Solution 4 - Android

The constant value used is wrong. It should be 8 for GONE. 4 for INVISIBLE and 0 for VISIBLE.

Check this View description from the developer's site.

And this link.

Solution 5 - Android

Use:

mainLayout.setVisibility(LinearLayout.INVISIBLE);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdhamView Question on Stackoverflow
Solution 1 - AndroidjamesView Answer on Stackoverflow
Solution 2 - AndroidMarc BernsteinView Answer on Stackoverflow
Solution 3 - AndroidShahul3DView Answer on Stackoverflow
Solution 4 - AndroidJosephView Answer on Stackoverflow
Solution 5 - Androidi18nView Answer on Stackoverflow