Problem with RelativeLayout when View view is View.GONE

I have a RelativeLayout like this:

 <RelativeLayout> <TextView1/> <TextView2/> // <-- View.VISIBLE OR View.GONE <TextView3/> <TextView4/> </RelativeLayout> 

Each TextView anchored below the previous TextView with android:layout_below .

The problem is that TextView2 may or may not be (either View.VISIBLE or View.GONE ); if it is View.VISIBLE , then everything is fine, but if it is View.GONE , then TextView3 ends with rendering on top of TextView1.

I tried various ways to fix this, but every time RelativeLayout catches me, you cannot refer to the identifier until it is defined.

I hope that I missed something obvious here.

+71
android relativelayout android-relativelayout
Jul 19 '10 at 7:15
source share
10 answers

You can place text 2 and 3 in LinearLayout and save the line layout below textview 1.

+39
Jul 19 '10 at 7:47 a.m.
source share
— -

You can use this tag:

 android:layout_alignWithParentIfMissing="true" 

From the docs:

If set to true, the parent will be used as an anchor if the binding cannot be found for layout_toLeftOf, layout_toRightOf, etc.

+176
Feb 15 2018-12-12T00:
source share

Why not update the below TextView3 attribute when updating the visibility of TextView2? (I assume you do this in code)

something like

 TextView tv = (TextView) findViewById(R.id.textview3); RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) tv.getLayoutParams(); lp.addRule(RelativeLayout.BELOW, R.id.textview1); ((TextView) view).setLayoutParams(lp); 
+10
Jul 19 '10 at 7:23
source share

This answer does not solve your specific problem, but it solves a similar problem, so hopefully this helps someone.

I had a situation where my relative layout did not have the equivalent of your TextView1. So, in my situation, if TextView2 was GONE, then I wanted TextView3 to be aligned with the parent top. I decided that adding the android: layout_alignWithParentIfMissing = "true" attribute to TextView3. See http://developer.android.com/resources/articles/layout-tricks-efficiency.html .

Unfortunately, I see no way to specify an alternative alignment binding if it is not a parent.

+6
Jul 14 2018-11-11T00:
source share

Forget about INVISIBLE or GONE , use instead:

 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams(); params.height = 0; params.setMargins(0,0,0,0); view.setLayoutParams(params); 
+6
Sep 02 '11 at 18:14
source share

You can do it

 <RelativeLayout> <TextView1/> <FrameLayout> <TextView2/> // <-- View.VISIBLE OR View.GONE </FrameLayout> <TextView3/> <TextView4/> </RelativeLayout> 

Let TextView3 below this FrameLayout, which has no background, so if TextView2 is Gone, it does not take up space.

+2
Sep 07 '18 at 6:35
source share

If you are not using RelativeLayout : use LinearLayout .

+1
Sep 24 '14 at 6:09
source share

Place all text views below the LinearLayout with a vertical orientation.

 <LinearLayout> <TextView/> <TextView/> <TextView/> <TextView/> <TextView/> <TextView/> </LinearLayout> 
0
Sep 13 '18 at 12:43
source share

Set gravity for your parent layout

0
May 27 '19 at 9:49
source share

To do this, just play with alpha 0/1. and also disable onClickListener, if any

-2
Jun 03 '18 at 16:44
source share



All Articles