Reputation: 10988
I have a RelativeLayout as root element. There are three LinearLayouts under root element. First and last has fixed height and the middle one takes rest of screen.
There are two ScrollViews inside the middle LinearLayout. I programatically add new views in them. I want to show three item in a scrollview no matter the screen size is.
The problem is, I can't calculate the height of are so I can't divide it by three and get item height required.
I tried to call measure() and getMeasuredHeight() but LinearLayout returned 21 (which I have no idea why) and ScrollViews returned 0. Both LinearLayout and ScrollViews has match_parent attribute.
So how can I get the actual height? I know it is calculated somewhere because I can see it covers the all empty area.
Upvotes: 1
Views: 232
Reputation: 23432
The Good
Don't unless you have a really good reason. This kind of layout won't work for tablets and larger devices (it will look very odd having giant list items) and it is not common behaviour.
The Bad
You said the top and bottom views have a fixed size, and you can get the height of the screen with:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
// Use getSize on API13+
int displayHeight = display.getHeight();
Then the height of each row is just
int listHeight = displayHeight - (2 * fixedHeight)
int rowHeight = listHeight/3;
This is bad because it will not work in more complex layouts.
The Ugly
In onCreate
the size of your views will not yet have been initialised. You need wait until the view has been given a size. There a few ways you could do this:
View.OnLayoutChangeListener
(API 11+)ViewTreeObserver.OnGlobalLayoutListener
(API 1+)onSizeChanged
Upvotes: 2