Android: layout with match_parent shall take the space left or share

1.5k views Asked by At

I have a horizontal Linear Layout, containing two Layouts. I want the right layout to be as wide as the content (wrap_content). The left layout shall fill remaining space.

I tried with "match_parent" on the left layout(Relative Layout) and "wrap_content" on the right layout(Linear Layout), but the left layout takes all the space.

How can I solve this problem, that the left Layout just takes the space, which remains, not everything. Like letting the right layout take its space first.

EDIT:: Sorry, I wanted to post a picture, but couldnt, here is the code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:measureWithLargestChild="false">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00ff27"
    android:layout_gravity="bottom">
</RelativeLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:layout_alignParentStart="true">

    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_alignParentStart="true" />
</LinearLayout>

The Relative Layout on the left takes ALL the space (the screen becomes green). I want the Relative Layout to take the width, which remains from the LinearLayout, so you can see the button in the Layout.

2

There are 2 answers

1
RussHWolf On BEST ANSWER

Android lays out views in the order in which they appear in the layout file. Therefore, your first view fills up all available space before the second view has a chance to take up any space. One workaround is to make your root layout a RelativeLayout instead of a LinearLayout, letting you put the right-side view first. Another is to leave the root LinearLayout and use the layout_weight attribute. On your first view, instead of android:layout_width="match_parent", try android:layout_width="0dp" and android:layout_weight="1"

0
Aakash On
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="horizontal">

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:layout_weight="1"
        android:background="#00ff27"></RelativeLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:orientation="vertical">

        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="New Button" />
    </LinearLayout>
</LinearLayout>