Hide linear layout at runtime on Android

I have the following layout

<merge> <LinearLayout android:id="@+id/ll_main" android:layout_height="fill_parent" android:layout_width="fill_parent" /> <LinearLayout android:id="@+id/ll_sub" android:layout_height="fill_parent" android:layout_width="fill_parent" /> </merge> 

What I want to do is show / hide the ll_sub layout at runtime via setVisibility() , but it does not work.

When I set android:visibility="gone" (also I checked with invisible ) from xml ll_sub , it does not appear on the screen, and this time when I use setVisibility() to show this layout at runtime, it displays but when I try to hide this layout after showing it, it is not hiding.

EDIT

I am trying to show / hide this linear layout with the click of a button.

 LinearLayout ll; Button minimize; int visibility=0; @Override public void onCreate(Bundle savedInstanceState) { ll=(LinearLayout)findViewById(R.id.ll_sub); minimize=(Button)findViewById(R.id.minimize); minimize.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { if(visibility==0) { visibility=2; } else { visibility=0; } ll.setVisibility(visibility); } }); } 
+4
android android-layout
Apr 13 2018-11-11T00:
source share
2 answers

It looks like you are setting the wrong constants to change the visibility view .

 GONE == 8 INVISIBLE == 4 VISIBLE == 0 

However, you should never rely on the actual values ​​that Android has named to indicate its constants. Instead, use the values ​​defined in the view class: View.VISIBLE , View.INVISIBLE and View.GONE .

 // snip... if(visibility == View.VISIBLE) { visibility = View.GONE; } else { visibility = View.VISIBLE; } ll.setVisibility(visibility); 

And don't forget to call invalidate() in the view :)

+16
Apr 13 2018-11-11T00:
source share

You must use the constants provided by View

 View.INVISBLE, View.VISIBLE, View.GONE 

and also cancel your view

+2
Apr 13 '11 at 6:00
source share



All Articles