James
James

Reputation: 3749

Issue with RelativeLayout when View visibility is View.GONE

I've a RelativeLayout thus:

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

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

The problem is that TextView2 may or may not be there (either View.VISIBLE or View.GONE); if it's View.VISIBLE, then all is fine, but if it's View.GONE, then TextView3 ends up being rendered on top of TextView1.

I've tried various ways to fix this, but each time am caught out by RelativeLayout's 'you cannot reference an id before it's defined' rule.

I'm hoping that I'm missing something obvious here.

Upvotes: 91

Views: 32887

Answers (9)

Dmitry K
Dmitry K

Reputation: 95

Use ConstraintLayout, it doesn't break if a referenced view becomes GONE, instead it works as if the size of the hidden view became zero.

https://developer.android.com/reference/androidx/constraintlayout/widget/ConstraintLayout#VisibilityBehavior

Upvotes: 0

Ashwini
Ashwini

Reputation: 703

place all textViews under LinearLayout with vertical orientation.

<LinearLayout>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
</LinearLayout>

Upvotes: 1

Alittle927
Alittle927

Reputation: 71

you can do this

<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 doesn't occupy space.

Upvotes: 7

Taldroid
Taldroid

Reputation: 380

A simple hack for this is to play with alpha 0/1. and also disable the onClickListener if there is any

Upvotes: -4

ininprsr
ininprsr

Reputation: 2500

You can use this tag:

android:layout_alignWithParentIfMissing="true"

From the docs:

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

Upvotes: 229

user925799
user925799

Reputation: 87

Forget about INVISIBLE or GONE, use this instead:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();

params.height = 0;

params.setMargins(0,0,0,0);

view.setLayoutParams(params);

Upvotes: 7

Samuel Yang
Samuel Yang

Reputation: 81

This answer does not solve your specific problem, but does solve a similar one, so hopefully this will help somebody.

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's top. I solved that by adding to TextView3 the attribute android:layout_alignWithParentIfMissing="true". See http://developer.android.com/resources/articles/layout-tricks-efficiency.html.

Unfortunately, I do not see a way to specify an alternate alignment anchor unless it is the parent.

Upvotes: 8

Karan
Karan

Reputation: 12782

You can place textview 2 and 3 in the LinearLayout and keep the linear layout below textview 1.

Upvotes: 43

slup
slup

Reputation: 5534

why not update the below attribute of TextView3 when you update 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);

Upvotes: 12

Related Questions