Java Enthusiast
Java Enthusiast

Reputation: 1191

How to make a view fill the extra space in a row?

In android app development, with Linear Layout, I have two buttons side-by-side in a row. I made such an arrangement with TableRow and center aligned it. But the buttons dont fill completely the extra space. How do I make the buttons fill the extra space to the left and to the right?

Here the xml code:

  <TableRow
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:layout_margin="7dp"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Save"
        android:textAlignment="center"
        android:id="@+id/save"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cancel"
        android:textAlignment="center"
        android:id="@+id/cancel"
        />
</TableRow>

The LinearLayout has the following attributes:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:layout_alignParentBottom="true"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context="com.appname.classname">

Here is the sample screen shot:

enter image description here

Upvotes: 0

Views: 52

Answers (2)

Bojan Kseneman
Bojan Kseneman

Reputation: 15668

Set the android:layout_marginRight of the first button to "-8dip" or even more. The sspace should be smaller or gone. You can use RelativeLayout, there are no margins there.

Upvotes: 0

ucsunil
ucsunil

Reputation: 7494

Instead of using the TableRow, use an horizontal LinearLayout inside the vertical LinearLayout. Note that the default orientation of LinearLayout is horizontal. So you can do soemthing like:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/save"
        android:text="@string/save"
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/cancel"
        android:text="@string/cancel"
        android:layout_weight="1"
        android:layout_width="0dip"
        android:layout_height="wrap_content" />
</LinearLayout>

The layout_weight attribute will make each item occupy the available space in that ration. In this case, the buttons will share the screen space in a 1:1 ratio.

Upvotes: 1

Related Questions