views:

34

answers:

1

Hi,

I have the following layout defined:

<LinearLayout>
  <TextView />
  <RelativeLayout>
     <TextView>
     <Button>
     <Button>
  </RelativeLayout>
<LinearLayout>

now i want to get the height of the RelativeLayout (i want to specify the height of its children programatically):

View rel = findViewById(R.id.RelativeLayout01);
int width = rel.getWidth();
int height = rel.getHeight();

but the size returned is 0. How do I get the size of the RelativeLayout (it's mostly pseudocode above, but both layout have a height/width of "fill_parent" in the xml)

+1  A: 

How do I get the size of the RelativeLayout

Your code is probably OK, but the timing is wrong. You are perhaps executing that code in onCreate(), which is too early, as the widgets have not been laid out on the screen yet.

The official answer is for you to extend RelativeLayout (or, perhaps, ViewGroup) and implement your own layout rules, using measure() and layout().

CommonsWare