Graeme
Graeme

Reputation: 25864

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

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.

Upvotes: 10

Views: 5569

Answers (5)

ania
ania

Reputation: 2350

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>

Upvotes: 2

Gregory
Gregory

Reputation: 4424

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.

Upvotes: 12

Tony
Tony

Reputation: 1576

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>

Upvotes: 2

deepa
deepa

Reputation: 2494

I think it help you..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:background="@drawable/bg"
 >

 <Button
 android:id="@+id/button"
 android:layout_width="wrap_content"
 android:layout_height="60dip"
 android:text="button"

/>
<Button
 android:id="@+id/button1"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_weight="1"
 android:layout_below="@+id/button"
 android:text="button1"

/>


</RelativeLayout>

Upvotes: 0

Vinay
Vinay

Reputation: 2415

It should be the other way..
first you should have the fixed element and then the match_parent or fill_parent

Upvotes: -1

Related Questions