Can't get rows in TableLayout to center horizontally

bieno002 picture bieno002 · Apr 4, 2012 · Viewed 12.9k times · Source

I can't get the rows in my TableLayout to center on the screen. I'm trying to create a 4x4 table of clickable images and want them centered on the screen. It ends of drawing them left aligned, I would like them to be centered with space on either side. Here is the code I have:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20dp">

        <TableRow android:layout_gravity="center_horizontal">

            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp"
                android:layout_gravity="center" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" 
                android:layout_gravity="center" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" 
                android:layout_gravity="center" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" 
                android:layout_gravity="center" />

        </TableRow>

        <TableRow android:layout_gravity="center_horizontal">

            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />

        </TableRow>

        <TableRow android:layout_gravity="center_horizontal">

            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />
            <ImageView android:src="@drawable/rocket"
                android:layout_width="60dp"
                android:layout_height="60dp" 
                android:padding="3dp" />            
        </TableRow>

    </TableLayout>


</LinearLayout>

Any suggestion?

Answer

Ramachandra Reddy Avula picture Ramachandra Reddy Avula · Apr 4, 2012

try with android:layout_gravity="center_horizontal" as follows

 <TableRow android:layout_gravity="center_horizontal" 
          android:layout_weightsum = "4">

                <ImageView android:src="@drawable/rocket"
                    android:layout_width="60dp"
                    android:layout_height="60dp" 
                    android:padding="3dp"
                    android:layout_weight = "1" />
                <ImageView android:src="@drawable/rocket"
                    android:layout_width="60dp"
                    android:layout_height="60dp" 
                    android:padding="3dp"
                    android:layout_weight = "1"  />
                <ImageView android:src="@drawable/rocket"
                    android:layout_width="60dp"
                    android:layout_height="60dp" 
                    android:padding="3dp"
                    android:layout_weight = "1"  />
                <ImageView android:src="@drawable/rocket"
                    android:layout_width="60dp"
                    android:layout_height="60dp" 
                    android:padding="3dp"
                    android:layout_weight = "1"  />            
            </TableRow>