1

I was told that I could use a second linear layout in such a way that my buttons would stretch to fill the remaining empty space vertically. I'm not sure where to go from here. Any help would be appreciated.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center">
    <TableRow>
        <Button
            android:id="@+id/one"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="1" />
        <Button
            android:id="@+id/two"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="2" />
    </TableRow>
    <TableRow>
        <Button
            android:id="@+id/three"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="3" />
        <Button
            android:id="@+id/four"
            android:text="4"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:layout_weight="1" />
    </TableRow>
    <TableRow>
        <Button
            android:id="@+id/five"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="5" />
        <Button
            android:id="@+id/six"
            android:text="6"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:layout_weight="1" />
    </TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
double-beep
  • 5,031
  • 17
  • 33
  • 41
user2593697
  • 29
  • 2
  • 6

2 Answers2

3

I'm assuming you want the buttons to fill up the available height on the screen. In that case please look to the answer here Android TableLayout height does not fills the whole screen

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center" >

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/one"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="1" />

            <Button
                android:id="@+id/two"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="2" />
        </TableRow>

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/three"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="3" />

            <Button
                android:id="@+id/four"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="4" />
        </TableRow>

        <TableRow
            android:layout_height="0dp"
            android:layout_weight="1" >

            <Button
                android:id="@+id/five"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="5" />

            <Button
                android:id="@+id/six"
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:text="6" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Notice that the table rows now have

 android:layout_height="0dp"
 android:layout_weight="1"

This will allow the row to expand to fill the remaining space (with each row getting 1/3 of the screen height).

I also changed the buttons layout width to 0dp, as you are using weight. Also you do not need the xmlns:android="http://schemas.android.com/apk/res/android" 3 times. You only need it on the parent layout.

Finally, as many of your buttons are using the same styling. I'd suggest you look at possibly using a style for these buttons. You can learn more about styles / themes at http://developer.android.com/guide/topics/ui/themes.html.

Community
  • 1
  • 1
Nicholas
  • 723
  • 5
  • 11
2

I think the function you are looking for is android:weightSum. Here is the official reference: http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:weightSum

So to expand your table layout to full screen you need to assign a weightSum of 3 to your TableLayout and divide android:layout_weight of 1 to each TableRow. That will give you the desired result.

So your xml layout file will look like this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
<LinearLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="horizontal" >
<TableLayout

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="3"
    android:gravity="center">
    <TableRow
        android:layout_weight="1">
        <Button
            android:id="@+id/one"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="fill_vertical"
            android:layout_weight="1"
            android:gravity="center"
            android:text="1" />
        <Button
            android:id="@+id/two"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="2" />
</TableRow>
<TableRow
    android:layout_weight="1">
    <Button
        android:id="@+id/three"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="fill_vertical"
        android:layout_weight="1"
        android:gravity="center"
        android:text="3" />
    <Button
        android:id="@+id/four"
        android:text="4"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_weight="1" />
</TableRow>
<TableRow
    android:layout_weight="1">
    <Button
        android:id="@+id/five"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="5" />
    <Button
        android:id="@+id/six"
        android:text="6"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_weight="1" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Manu
  • 81
  • 1
  • 5
  • It should be an answer, not a comment. – Androiderson Jul 26 '13 at 01:32
  • Hello Mr.Exception-al I don't have 50 rep as of now to put a comment on his question to elaborate on his question. What you suggent I do. Nothing? Above it your -ve vote is not helping the cause either. Thanks! – Manu Jul 26 '13 at 01:36
  • Hi Manu, unfortunately the rules of this site states that you should do nothing in this case. – Androiderson Jul 26 '13 at 01:43
  • Thanks for letting me know that. Will update my answer with something that I think he is looking for. :) – Manu Jul 26 '13 at 01:50
  • Meanwhile I suggest you delete this answer (so you don't lose reputation) and post a new one when you come up with a solution that can help the OP. – Androiderson Jul 26 '13 at 01:54
  • Hello, Exception-al I have updated my answer with something which the asker might be looking for. Thanks! – Manu Jul 26 '13 at 02:14