Marco Lau
Marco Lau

Reputation: 619

Center a TextView in a TableRow in a TableLayout

I am trying to build something like a header. I tried using android:gravity="center" and android:textAlignment="center" on the TextView but it doesn't center the text. I don't know where the problem is.

<TableLayout 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"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#008000" >

    <TextView
        android:id="@+id/HeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        <!--android:textAlignment="center"
        android:gravity="center"-->
        android:text="@string/header"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF"
        android:textStyle="italic" />

</TableRow>

</TableLayout>

Upvotes: 10

Views: 29782

Answers (4)

KDeogharkar
KDeogharkar

Reputation: 10959

give android:gravity="center" to TableRow.

Upvotes: 18

Tek Nath
Tek Nath

Reputation: 1779

If you have many TextView inside TableRow doing this solved my problem

    <TableRow
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/feedback" />

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/football" />

            <TextView
                android:layout_width="0dp"
                android:gravity="center"
                android:layout_height="wrap_content"
                android:layout_weight="0.33"
                android:text="@string/about" />

        </TableRow>

Upvotes: 0

Query
Query

Reputation: 625

Set gravity to table row

<TableLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content">

           <TableRow 
                android:gravity="center_horizontal"/>
</TableLayout>

Upvotes: 2

Shivang Trivedi
Shivang Trivedi

Reputation: 2182

<TableLayout 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"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:background="#008000" >

    <TextView
        android:id="@+id/HeaderTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="header"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFF"
        android:textStyle="italic" />
</TableRow>

Upvotes: 18

Related Questions