0

I am writing code for an activity that has a table that initially contains 2 rows(see screenshot). I would like the text in those 2 rows to be in the center(not the TextView).

However, setting android:gravity = "center" or android:gravity = "center_horizontal" does not affect the text at all.
What am I missing?

Screenshot

activity_game.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal"
android:padding="16dp"
tools:context=".MainActivity">

<TableLayout
    android:id="@+id/tl1"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <TableRow
        android:id="@+id/tr_header_p1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_row_bg"
        android:padding="5dp">

        <TextView
            android:id="@+id/player1_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Player 1"
            android:textColor="#000000"
            android:textSize="24sp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_row_bg"
        android:padding="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/table_cell_bg"
            android:gravity="center"
            android:text="Guess"
            android:textColor="#000000"
            android:textSize="16sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/table_cell_bg"
            android:gravity="center"
            android:text="Bulls"
            android:textColor="#000000"
            android:textSize="16sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Cows"
            android:textColor="#000000"
            android:textSize="16sp" />
    </TableRow>
</TableLayout>

<TableLayout
    android:id="@+id/tl2"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1">

    <TableRow
        android:id="@+id/tr_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_row_bg"
        android:padding="5dp">

        <TextView
            android:id="@+id/player2_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Player 2"
            android:textColor="#000000"
            android:textSize="24sp" />
    </TableRow>

    <TableRow
        android:id="@+id/tr_header2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_row_bg"
        android:padding="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/table_cell_bg"
            android:gravity="center"
            android:text="Guess"
            android:textColor="#000000"
            android:textSize="16sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/table_cell_bg"
            android:gravity="center"
            android:text="Bulls"
            android:textColor="#000000"
            android:textSize="16sp" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Cows"
            android:textColor="#000000"
            android:textSize="16sp" />
    </TableRow>

</TableLayout>

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
Sajal Narang
  • 397
  • 1
  • 14

6 Answers6

1

Try this should work,

add android:gravity="center" to your @+id/tr_header_p1 and @+id/tr_header like,

<TableRow
    android:id="@+id/tr_header_p1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp">

and

<TableRow
    android:id="@+id/tr_header"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:padding="5dp">

this might helps you

EDIT 1

or you need to mention the android:layout_weight="1" to your TextView. like

 <TextView
        android:id="@+id/player2_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Player 2"
        android:textColor="#000000"
        android:textSize="24sp" />
Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
  • Works. Can you explain why? Shouldn't this only shift the TextView to the center but since the layout_width of the TextView is match_parent, it should not have an effect at all. – Sajal Narang Jul 06 '16 at 10:54
  • If my answer helps you means kindly mark as answer and close the question . – Sathish Kumar J Jul 06 '16 at 11:05
  • 1
    your parent TableLayout having weight=1 but , you didn't mention any weight to your child. if u give same weight (1) to your child means , the problem will be solve – Sathish Kumar J Jul 06 '16 at 11:11
0

try for the TableRow parent of TextView

android:gravity="CENTER_HORIZONTAL"

the default layout of a table it's applied to children nodes, as described in documentation here

ddb
  • 2,423
  • 7
  • 28
  • 38
  • @SajalNarang I changed the answer, apply `center_horizontal` to the parent of those textviews – ddb Jul 06 '16 at 10:51
  • Adding the android:gravity attribute to the parent does the trick, but I don't understand why. As far as I know, it will try aligning the TextView to the center of the TableRow, but fail because the layout_width of the TextView is set to match_parent. Why does it have any effect on the contents of the TextView? – Sajal Narang Jul 06 '16 at 11:04
  • @SajalNarang, as in html language, the default layout of a table it's applied to children nodes – ddb Jul 06 '16 at 11:11
  • then shouldn't my code do the same thing? I am just applying the layout directly to the child. – Sajal Narang Jul 06 '16 at 11:13
  • but if you do it in the html, it will not work :) usually you apply css style on the tablerow, not in the spans and div inside the table cell (I'm talking about layout styles – ddb Jul 06 '16 at 11:14
0

Try this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="horizontal"
    android:padding="16dp"
    tools:context=".MainActivity">

    <TableLayout
        android:id="@+id/tl1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TableRow
            android:id="@+id/tr_header_p1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="@drawable/table_row_bg"
            android:padding="5dp">

            <TextView
                android:id="@+id/player1_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"

                android:text="Player 1"
                android:textColor="#000000"
                android:textSize="24sp" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/table_cell_bg"
                android:gravity="center"
                android:text="Guess"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/table_cell_bg"
                android:gravity="center"
                android:text="Bulls"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Cows"
                android:textColor="#000000"
                android:textSize="16sp" />
        </TableRow>
    </TableLayout>

    <TableLayout
        android:id="@+id/tl2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <TableRow
            android:id="@+id/tr_header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="@drawable/table_row_bg"
            android:padding="5dp">

            <TextView
                android:id="@+id/player2_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="Player 2"
                android:textColor="#000000"
                android:textSize="24sp" />
        </TableRow>

        <TableRow
            android:id="@+id/tr_header2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/table_row_bg"
            android:padding="5dp">

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/table_cell_bg"
                android:gravity="center"
                android:text="Guess"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/table_cell_bg"
                android:gravity="center"
                android:text="Bulls"
                android:textColor="#000000"
                android:textSize="16sp" />

            <TextView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Cows"
                android:textColor="#000000"
                android:textSize="16sp" />
        </TableRow>

    </TableLayout>
</LinearLayout>
AhmadReza Payan
  • 2,171
  • 1
  • 23
  • 33
0

You can try setting layout_width to wrap_content and then set layout_gravity to center.

That align the entire TextView and not the text inside it

rsella
  • 337
  • 1
  • 7
0

What if you used android:layout_gravity from your TextView which is gonna affect the position of the element according its parent? There's a thread explaining the difference between both below

Gravity and layout_gravity on Android

Community
  • 1
  • 1
Victor Hugo Montes
  • 1,270
  • 1
  • 17
  • 28
0
<TableRow
        android:id="@+id/tr_header"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/table_row_bg"
        android:gravity="center_horizontal"
        android:padding="5dp">

        <TextView
            android:id="@+id/player2_name"
            android:layout_width="match_parent"
            android:gravity="center"
            android:text="Player 2"
            android:textColor="#000000"
            android:textSize="24sp" />
    </TableRow>

This is working for me

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32