Search code examples
androidandroid-tablelayout

Remove gaps between buttons in an Android TableLayout


I want to have a square which contains four buttons with no gaps between them and perhaps a border around the four buttons, I have been experimenting with table layouts as I feel these are easier to control layouts. I have reverted back to my original layout for this question as my latest attempt was a mess. You can see just now I have two buttons per row, I feel there should be four but it goes all wonky when I put four in. Here is my layout:

<TableRow
    android:layout_gravity="center_horizontal"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal" />
</TableRow>

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

    <Button
        android:id="@+id/button3"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_column="0"
        android:layout_weight="1"
        android:gravity="center_horizontal" />
</TableRow>

Can anyone explain to me how I can achieve what I want? I also tried nesting table rows which of course failed. The documentation only further confused me, any advice is appreciated!


Solution

  • Set margins between buttons and tableRows to negative value like this:

        <Button
            android:id="@+id/id1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/id1"
            android:layout_marginBottom="-7dp" />