I’ve looked through so many threads here trying to understand why this is happening, but I just cannot wrap my head around it.
In my RelativeLayout I have 4 HorizontalScrollViews (HSV) that lay above a menu (which is alignedParentBottom).
I want to evenly space the 4 HSVs above this menu.
By my calculations, I should be able to take the device height, subtract the menu height, and then divide by 4, to get an individual HSV height.
However, when I set the height of each HSV, they are larger than they should be.
(note: display.getHeight() returns the correct dimension. I am on a 480×800 device, and if I print deviceHeight it returns 800)
Here is my code:
RelativeLayout menuLayout = (RelativeLayout)findViewById(R.id.menuLayout);
HorizontalScrollView row1 = (HorizontalScrollView)findViewById(R.id.row1);
HorizontalScrollView row2 = (HorizontalScrollView)findViewById(R.id.row2);
HorizontalScrollView row3 = (HorizontalScrollView)findViewById(R.id.row3);
HorizontalScrollView row4 = (HorizontalScrollView)findViewById(R.id.row4);
//get current device dimensions
Display display = getWindowManager().getDefaultDisplay();
deviceWidth = display.getWidth(); //returns 480
deviceHeight = display.getHeight(); //returns 800
int menuHeight = deviceWidth/5; // 480/5 = 96
int remainingSpace = deviceHeight - menuHeight; // 800 - 96 = 704
int rowHeight = (int) (remainingSpace/4); // 704/4 = 176
menuLayout.getLayoutParams().height = menuHeight;
row1.getLayoutParams().height = rowHeight;
row2.getLayoutParams().height = rowHeight;
row3.getLayoutParams().height = rowHeight;
row4.getLayoutParams().height = rowHeight;
When I run this code, each row is too large, and gets pushed under the menu at the bottom. If I run getHeight(). on a row, it says it is the correct height (176), but clearly it is not (as the 4 rows are too large to fit in the space above the menu).
Can anyone shed a light on this? Thank you so much!
I wasn’t considering that there is the menu bar at the top, which takes up part of the devices screen. My solution is to have the main layout height be fill_parent, and then to get the height of the main layout. I use this value, instead of the display.getHeight() value.