how to hide linearlayout from java code?
AndroidAndroid 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);