romamnmlst
romamnmlst

Reputation: 283

Sizing GUI elements by percentage in-line

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 480x800 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!

Upvotes: 0

Views: 165

Answers (3)

romamnmlst
romamnmlst

Reputation: 283

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.

Upvotes: 0

Paul Burke
Paul Burke

Reputation: 25584

You should really use a LinearLayout here. You can use the android:layout_weight attribute to use percentages. If things are even, simply use 1, otherwise, you can set android:weightSum="100" and then use whole numbers (as percentages) in the weight.

E.g. android:layout_weight="25" for 25%, when the parent has android:weightSum="100".

Upvotes: 2

nicephotog
nicephotog

Reputation: 1

ViewGroup.LayoutParams "constant field value" for the nested subclass LayoutParams public static final int FILL_PARENT Constant Value: -1 (0xffffffff)

It says the view should be as big as the parent minus padding, however you could require to put another "parent" inside the parent bounding the whole set to contain the separate set you resized.

Upvotes: 0

Related Questions