0

I have a TableLayout that contains only one row and one cell. I would like the content of my cell to be horizontally centered. I tried two approaches :

First method, I set the attribute android:gravity="center_horizontal" on the TableRow element and it works as expected :

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TableRow android:gravity="center_horizontal">
            <Button
                android:id="@+id/MyButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"            
                android:text="@string/Hello" />
        </TableRow>
    </TableLayout>

Second method, I set the attribute android:layout_gravity="center_horizontal" directly on the cell content (the button here) but that doesn't centers the button :

<?xml version="1.0" encoding="utf-8"?>
        <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TableRow>
                <Button
                    android:id="@+id/MyButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"            
                    android:layout_gravity="center_horizontal"
                    android:text="@string/Hello" />
            </TableRow>
        </TableLayout>

Can someone explain me why the second approach doesn't work ?

Thank you

Riana

Riana
  • 689
  • 6
  • 22

1 Answers1

0

android:gravity refers to the child content inside the view. In your case, this affects the CONTENTS of the table cell.

android:layout_gravity refers to the placement of the view in its parent. In this case, the placement of the cell in the table, but NOT the cell contents. The cell contents will NOT be affected.

See this similar question for some other examples and further explanation: Gravity and layout_gravity on Android

Community
  • 1
  • 1
joshgoldeneagle
  • 4,616
  • 2
  • 23
  • 30
  • Hi and thanks for your answer. In my examples, I understand why the first approach works. What is not clear is why the second one fails .. the layout_gravity attribute applied to the button should centers the button inside its parent view (the TableRow) – Riana May 06 '16 at 14:25
  • I don't understand how this is not clear. Gravity is for the child content of the table cell. Layout_gravity is for the placement of the table cell itself in its parent. Just as explained in the answer. – joshgoldeneagle May 06 '16 at 19:22
  • In my example, the Button is the child of the TableRow. When I set the "gravity" attribute on the TableRow then that centers its content, here the button. But when I set the "layout_gravity" attribute on the Button, I expect it also to centers itself inside its container, i.e the TableRow. I thougt setting the "gravity" on the container or setting "layout_gravity" on the content should have the same behavior. I hope I'm more clear .. – Riana May 09 '16 at 08:55
  • Definitely should NOT have the same behavior. Layout gravity and gravity are different. Perhaps someone else can explain it better than myself. – joshgoldeneagle May 10 '16 at 21:42
  • Thanks for answers. Setting the "gravity" attribute on the TableRow will centers all the cells of the TableRow. What I would like to achieve is to be able to centers the content of the first column and align to right the content of a second column if my TableRow contains two columns (cells) .. I can't achieve that by using "gravity" on the TableRow. – Riana May 11 '16 at 11:55