views:

652

answers:

3

I've tried unsuccessfully for quite a few hours now to simply get a TableLayout to scale to the full screen. I've tried stretchColumns, shrinkColumns, (specifying the column index or using *) you name it. It's almost as if the parent element (TableLayout) is completely ignoring 'android:layout_width="fill_parent"'.

I've read through similar questions here, though they haven't solved it for me. The XML below has 3 rows, and the table always shrink wraps to the size of the content.

Apparently I'm too new to post pictures(?) Here is the XML rendered into the project i'm working on. http://www.dashiva.com/images/Capture.png

What am I missing here, it's quite frustrating!

<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" android:stretchColumns="*">

<TableRow>
    <TextView android:id="@+id/AmountText" android:text="@string/general_amount" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/CardText" android:text="@string/general_card" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="@string/general_date" android:id="@+id/DateText" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/ErrorText" android:text="@string/general_error" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
</TableRow>

<TableRow>
    <TextView android:text="Amount here..." android:id="@+id/AmountNum" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:id="@+id/CardNum" android:text="Num here..." android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="date here..." android:id="@+id/DateDate" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
    <TextView android:text="Code here..." android:id="@+id/ErrorCode" android:paddingRight="8dip" android:layout_gravity="center"></TextView>
</TableRow>

<TableRow>
    <ImageView android:id="@+id/cc_image" android:src="@drawable/mastercard" android:background="@drawable/row_bg2" android:layout_gravity="center" android:layout_weight="1"></ImageView>
    <TextView android:id="@+id/ResponseText" android:layout_span="4" android:text="Response text here..." android:textSize="18dip" android:layout_weight="1"></TextView>
</TableRow>

<ImageView android:layout_gravity="center_vertical" android:background="@drawable/gradient_bg" android:layout_height="3dip" android:layout_width="fill_parent"></ImageView>
</TableLayout>
A: 

Well, here is what I can tell you:

  1. I removed your ImageView elements (since I didn't have your images) and the TableLayout fills the width of the screen quite nicely.
  2. Your last <TableRow> claims to have five columns (one for the ImageView and four for the TextView), whereas the other rows have four.

So, I would try changing android:layout_span="4" to android:layout_span="3" and see if that helps.

CommonsWare
A: 

Ok, the issue was where I Programmatically inflated the XML. For some reason, I was inflating it into a view then adding it to a TableRow, then adding that to a parent view. Removing the middle step and just adding the inflated view to the parent element fixed the issue.

Thanks though, I was under the impression the span was a simple count, not zero-based.

Rendrik
The span should be a count.
Romain Guy
A: 

If layout_weight is not set the stretching will also not work. This was not the problem in your case, but maybe it helps someone else that lands here.

(docs/guide/topics/ui/layout-objects.html says: "Child views can specify an integer weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.)"

Binks