Right align column in android table layout

Ankit HTech picture Ankit HTech · Oct 15, 2012 · Viewed 29.6k times · Source

I want to draw a table in which last column should be at the right most side of the table.

This is how the table row looks like:

Admin (2)New
Network (2)New

And this is how it should be:

Admin (2)         New
Network (2)       New

XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- row.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="45dp"
    android:gravity="center" android:background="@color/list_bg">

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

        <TableRow>



            <ImageView android:id="@+id/t1" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView android:id="@+id/t2" android:typeface="normal"
                android:singleLine="true" android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t10" android:typeface="normal"
                android:singleLine="true" android:text=" " android:textSize="14sp"
                android:textStyle="normal" android:layout_width="wrap_content"
                android:textColor="#000000" android:layout_height="wrap_content" />
            <TextView android:id="@+id/t4" android:typeface="normal"
                android:visibility="gone" android:singleLine="true" android:text="("
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t5" android:typeface="normal"
                android:visibility="gone" android:singleLine="true"
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t6" android:typeface="normal"
                android:visibility="gone" android:singleLine="true" android:text=")"
                android:textSize="14sp" android:textStyle="normal"
                android:layout_width="wrap_content" android:textColor="#000000"
                android:layout_height="wrap_content" />
            <ImageView android:id="@+id/t3" android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
            <TextView android:id="@+id/t7" android:typeface="normal"
                android:visibility="visible" android:singleLine="true" android:text="New"
                android:textSize="14sp"
                android:textStyle="normal" android:layout_width="wrap_content"
                android:textColor="#000000" android:layout_height="wrap_content" />

        </TableRow>

    </TableLayout>

</RelativeLayout>

In this xml t7 should be right at right most side of the table, how to do this???

Answer

Cookster picture Cookster · Oct 15, 2012
  1. android:stretchColumns="x" where x is the column you want to fill
  2. android:layout_width="fill_parent" to ensure everything fills the screen
  3. android:layout_gravity="right" for the view component in column "x"

example (pseudo code):

<TableLayout
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:stretchColumns="1">

    <TableRow>
        <TextView android:text="Left Column" />
        <TextView android:layout_gravity="right|center_vertical" android:text="Right Adjusted Column" />
    </TableRow>

</TableLayout>