Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make Views fill the full width of their parent in my Android app?

I have the following layout defined for one of my Activities:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout android:id="@+id/TableLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow android:id="@+id/TableRow01" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <EditText android:text="Resource Name" android:id="@+id/ResourceName" android:lines="1" android:isScrollContainer="false"></EditText>
    </TableRow>
    <TableRow android:id="@+id/TableRow02" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <Button android:id="@+id/Tile" android:text="Tile"></Button>
    </TableRow>
</TableLayout>

The layout renders almost correctly, the only problem is that my text box and my button aren't occupying the full width of their respective rows.

I've tried specifying fill_parent for the layout width properties, but to no avail, they still only occupy roughly half of the screen.

Documentation overall for Android so far has been great, but there are a few scenarios like this one where I hit an invisible wall! Thanks for all the help!

like image 907
Alexander Trauzzi Avatar asked Mar 08 '10 23:03

Alexander Trauzzi


People also ask

What is the linear layout in android?

LinearLayout is a view group that aligns all children in a single direction, vertically or horizontally. You can specify the layout direction with the android:orientation attribute. Note: For better performance and tooling support, you should instead build your layout with ConstraintLayout.

How can I get remaining height in android?

You can use set the layout_width or layout_width to 0dp (By the orientation you want to fill remaining space). Then use the layout_weight to make it fill remaining space.

What is gravity in linear layout?

To control how linear layout aligns all the views it contains, set a value for android:gravity . For example, the snippet above sets android:gravity to "center". The value you set affects both horizontal and vertical alignment of all child views within the single row or column.


2 Answers

Try using android:stretchColumns="0" (when you define the TableLayout) or what ever is the index of the column that you would want to stretch.

For more information - http://developer.android.com/guide/tutorials/views/hello-tablelayout.html

like image 99
Rahul Avatar answered Sep 21 '22 18:09

Rahul


Use android:layout_width="match_parent" in your tableRow views.

Now you have different possibilities in your TableLayout seting the parameter android:stretchColumns to:

Value "*" views will have same width and will fill the row occupying the same space

Value "0" or other integer -> First or element corresponding with the integer in a row will fill the row. The other view will have width in a same way of wrap_content

Example with a tablelayout of 3 tableRow with 2 views each:

Without setting android:stretchColumns:

[|TextView|EditText|          ]
[|TextView|EditText|          ]
[|TextView|EditText|          ]

android:stretchColumns="*"

[|  TextView    |  EditText  |]
[|  TextView    |  EditText  |]
[|  TextView    |  EditText  |]

android:stretchColumns="0"

[|    TextView      |EditText|]
[|    TextView      |EditText|]
[|    TextView      |EditText|]

android:stretchColumns="1"

[|TextView|      EditText    |]
[|TextView|      EditText    |]
[|TextView|      EditText    |]
like image 24
macfnl Avatar answered Sep 21 '22 18:09

macfnl