0

I have the following views in a table row:

<TableRow>
   <RelativeLayout>
       <RelativeLayout 
           android:id="@+id/back_layer"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>

       <LinearLayout 
           android:id="@+id/front_layer"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"/>

   </RelativeLayout>
</TableRow>

back_layer and front_layer overlap each other. Both should have the same size and fill the parent view. back_layer must be under front_layer thus back_layer is rendered before front_layer.

The problem is that front_layer has an arbitrary height. Because of the rendering order, there is no way for the back_layer to know the eventual height of the front_layer when the back_layer is rendered. front_layer has the same height as the parent. I am having a hard time making back_layer fill its parent height.

I am sure I can add back_layer after front_layer is added. Where would be the right place to do it (I tried to do it in the adapter and it does not work)? And how should I do it? I will need the reference of the table, and change the back_layer of every row in the table. Is that realistic?

YoYoMyo
  • 894
  • 12
  • 20
  • 1
    This is not a problem of rendering order. The layout pass (where all the view sizes and positions are determined) happens before anything is rendered. – Henry Jan 14 '15 at 19:10

3 Answers3

1

Wait for the front to be measured, then set the height of the back:

final LinearLayout front = (LinearLayout) findViewById(R.id.front_layer);
final RelativeLayout back = (RelativeLayout) findViewById(R.id.back_layer);
front.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
        public void onLayoutChange(View v, int l, int t, int r, int b, 
                                   int oldL, int oldT, int oldR, int oldB) {
        front.removeOnLayoutChangeListener(this);
        int height = front.getLayoutParams().height;
        back.setLayoutParams(new RelativeLayout
                .LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height));
    }
});
Simas
  • 43,548
  • 10
  • 88
  • 116
  • This is very similar to the solution I found on StackOverflow days later. Please see this post for the solution that worked for me: http://stackoverflow.com/questions/7733813/how-can-you-tell-when-a-layout-has-been-drawn – YoYoMyo Jan 22 '15 at 21:59
0

The wrapper relativelayout has no dimensions like width=match_parent and height=wrap_content. And if you want to remeasure something, make an own layout class and override the onmeasure method. Then you can measure the view yourself.

Mann
  • 661
  • 5
  • 9
0

If I'm supposing correctly your goal and the back_layer is like a background, you can simplify your layout in this way:

<TableRow>
    <FrameLayout
        android:id="@+id/back_layer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:id="@+id/front_layer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <!-- content -->

        </LinearLayout>
    </FrameLayout>
<TableRow>
bonnyz
  • 13,458
  • 5
  • 46
  • 70