views:

43

answers:

2

Hi

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. Any help much appreciated.

Cheers

James

A: 

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);
slup
+1  A: 

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

Karan
much smarter solution than the one I proposed!
slup