Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make a view fill the space available to it?

Tags:

android

layout

I know it must be an easy question... I just don't know how to fix it.

So (Just an example),

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:orientation="vertical">
    <Button     android:id="@+id/fakeButton"
                android:layout_height="match_parent"
                android:layout_width="match_parent"/>
    <Button     android:id="@+id/saveSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/saveSearch"/>
</LinearLayout>

This wouldn't work - the first button would make the 2nd invisible. Weights would make it all a percentage game which isn't what I want.

Am I being thick?

EDIT: I didn't know it, but it seem's the order is important (from answers). Loading a layout is iterative rather than holistic. You can make the first element a fixed height and the rest of the elements will fill what's left. BUT what I need is to make the final element a fixed height.

like image 831
Graeme Avatar asked Jul 19 '11 09:07

Graeme


3 Answers

You can use layout_weight exactly for that :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:orientation="vertical">
    <Button     android:id="@+id/fakeButton"
                android:layout_height="1"
                android:layout_width="match_parent"
                android:layout_weight="1.0" />
    <Button     android:id="@+id/saveSearch"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/saveSearch"/>
</LinearLayout>

With this, you'll have the bottom button have the basic height, and the fakeButton take up all the remaining space.

layout_height controls how the remaining space should be split between the different views. Default value is 0 (doesn't take any extra space). If you put both buttons to the same height and a weight of 1.0, then each button will take up 50% of the space.

like image 53
Gregory Avatar answered Oct 29 '22 01:10

Gregory


I think you want to make fakeButton to fill the rest space, right? You can use android:layout_weight to do this. Here is an example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:orientation="vertical">
<Button     android:id="@+id/fakeButton"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_weight="1"/>
<Button     android:id="@+id/saveSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/saveSearch"/>
</LinearLayout>
like image 42
Tony Avatar answered Oct 29 '22 01:10

Tony


Another, quite easy solution. Just use FrameLayout and margin property:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button     android:id="@+id/fakeButton"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:text="Fake button"
                android:layout_marginBottom="50dip" />

    <Button     android:id="@+id/saveSearch"
                android:layout_gravity="bottom"
                android:layout_width="fill_parent"
                android:layout_height="50dip"
                android:text="Save search"/>
</FrameLayout>
like image 25
ania Avatar answered Oct 29 '22 03:10

ania