0

I'm trying to figure out how to align the dashboardNewsItemDate to the right, instead of on the left. Currently it shows up immediately next to dashboardNewsItemHeadline. I thought that putting it within a TableRow would make the android:layout_gravity="right" function as I'd like, but that's proven to be wrong.

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_gravity="center">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemHeadline" android:layout_gravity="left"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemDate" android:layout_gravity="right" android:autoText="false"/>
    </TableRow>
</TableLayout>

In the end, my objective is for content so be displayed like this...

My news title                       Mar 3
-----------------------------------------
This is a bit of a longer news      Jul 2
title and it wraps
Ben
  • 60,438
  • 111
  • 314
  • 488

3 Answers3

0

You are close, try regular "gravity" instead of "layout_gravity" for your TextViews.

Booger
  • 18,579
  • 7
  • 55
  • 72
0

The layout_width="wrap_content" in the last TextView is cancelling out the gravity="right". Change it to fill_parent or match_parent and it should work.

0

There is a magic attribute which should solve your problem:

android:stretchColumns="*"

Add this attribute to your TableLayout:

<TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_gravity="center"
        android:stretchColumns="*">
    <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
        <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemHeadline" android:layout_gravity="left"
                />
        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/dashboardNewsItemDate" android:layout_gravity="right" android:autoText="false"/>
    </TableRow>
</TableLayout>
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83