views:

55

answers:

1

I am trying to create a basic datasheet in my application using a TableLayout. The first row contains the titles of each column. Every row thereafter is data. I would like the data to be scrollable while keeping the first title row visible (i.e. the first row would not scroll with the rest).

I have tried something like the following (unsuccessfully):

<TableLayout>
  <TableRow>
    <!-- The headers -->
    <TextView android:text="Header #1"/>
    <TextView android:text="Header #2"/>
    <TextView android:text="Header #3"/>
  </TableRow>

  <ScrollView>
    <!--The rest of the data -->
    <TableRow>
    <TableRow>
    ...
  </ScrollView>
</TableLayout>

This causes the program to crash. Any other suggestions?

A: 

It crashes because you can only have TableRows as direct descendants in a TableLayout.

What you probably want to use is a ListView and a 'header' above it.

Example (not complete):

<LinearLayout
  android:orientation="horizontal"
  android:layout_width="fill_parent"
>
   <TextView
      android:id="@+id/header1"
      android:layout_weight="1"
   />
   <TextView
      android:id="@+id/header2"
      android:layout_weight="1"
   />
   <TextView
      android:id="@+id/header3"
      android:layout_weight="1"
   />
</LinearLayout>
<ListView
    android:layout_width="fill_parent"
...
/>

The key is the layout_weight="1" and the android:layout_width="fill_parent" that will give you 3 views of equal size. You'll then need to create an Adapter and in your custom layout provided by the Adapter do the same thing for the child views. You might have to fiddle slightly with the padding/margins for them to line up horizontally exactly.

Qberticus
I have tried something similar to this. Will give it another shot in the morning and let you know how it turned out.
dfetter88
Okay, it seems I've managed to work something out. One more question: is there a way to manipulate 'layout_weight' so that one of the columns is a bit wider than the others? (maybe 1.25x wider than the other columns would do the job).
dfetter88
Setting the layout_weight to a lower value (such as .75) did the trick. Marking as solved.
dfetter88