Can't get rows in TableLayout to center horizontally

Go To StackoverFlow.com

2

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?

2012-04-04 02:33
by bieno002
try changing to

if that doesn't work I would add android:width="wrap_content" to the table rows and see if that work - ByteMe 2012-04-04 02:40

Changing android:layout_gravity to android:gravity did the trick! I can't believe I didn't try that one but thanks! Any ideas on what the difference is - bieno002 2012-04-04 03:13


9

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>
2012-04-04 11:34
by Ramachandra Reddy Avula
android:layout_weightsum - seems deprecate - NBaua 2017-11-22 06:35


5

Usually we use layout_gravity to align.

try

android:gravity="center_horizontal"

in your code.

android:gravity aligns the data to horizontal center.

2012-04-04 02:43
by Shankar Agarwal


2

In your case, bieno002, make sure your <TableLayout> has layout_width set to "wrap_content". This will give it space to breathe on either side.

Then, just add a android:layout_gravity="horizontal" to your <TableLayout> node. Like so:

<TableLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal">
    <TableRow>
        <TextView (or whatever)>
        <TextView (or whatever)>
    </TableRow>
</TableLayout>
2015-01-28 15:12
by alexismorin
Ads