0

Possible Duplicate:
Android table view

I have pretty much the same question as was asked here a year ago - how to implement table view widget in a nice manner, like on this screenshot. There wasn't a good answer to that question, so reposting.

I used ListView with a LinearLayout as its row, which contains several TextView elements and an ImageView. But the trouble is that each row is completely independent from the others in terms of column width. If a column in one row has text that's longer than in another, the two won't be aligned vertically. And GridView doesn't seem to be appropriate for my case, because it is meant to display homogenious data that is stored in a list and just display it in the columns - like grid of pictures. Any idea how this could be implemented? Thanks.

Update. OK, I've just found a solution here. In my Linear layout for a table row, despite setting android:layout_weight I was mistakenly setting android:layout_width to wrap_content. Setting it to 0dip made all TextView's in the LinearLayout have the same width in every row. Here's the example of my row:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:padding="6dip">
<TextView
    android:id="@+id/toptext"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="2"
    android:textSize="24sp"
/>
<TextView
    android:id="@+id/bottomtext"
    android:layout_width="0dip"
    android:layout_height="fill_parent"
    android:layout_weight="3"
    android:textSize="24sp" 
/>
<ImageView
    android:id="@+id/play"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:src="@drawable/play_1"
    android:clickable="true"
/>
</LinearLayout>
Community
  • 1
  • 1
SimpleMan
  • 329
  • 7
  • 18
  • does jquery mobile help? just a thought – visista Dec 12 '11 at 17:24
  • You should read up on layouts. RelativeLayout, LinearLayout, GridLayout etc. They are combined to make the layout you see there. I can give you some clues (I haven't coded android for months...): You can obviously see that they used a GridLayout with 5 columns in that case (or Layouts where they have assigned weight to each column) Then probably just substring the text to max 4 characthers or whatever you like and fit. It is many ways of achieving this – LuckyLuke Dec 12 '11 at 17:24
  • As it was pointed out by GrAnd Layouts aren't meant to display large amounts of data, that is my case unfortunately. @visista I initially tried Sencha Touch with phonegap for this project, but the amount of RAM the prototype used was unacceptable(22Mb), so I decided to go native. – SimpleMan Dec 12 '11 at 17:44

1 Answers1

0

To calculate desired widths of cells the table must know all its rows and cells widths based on data. The ListView does not know all info at once, so it can't do such calculations. The are two solutions, IMHO.

  1. Define fixed widths for ListView row items. This approach will consume as low resources as can and capable support about unlimited lists.
  2. Make TableLayout to behave like ListView (at least visually). Here is my answer how to implement it. But this solution is fine only if the table does not have a lot of rows (<100 is ok, I think), because it will load all date at once, even the invisible in a particular time.
Community
  • 1
  • 1
GrAnd
  • 10,141
  • 3
  • 31
  • 43
  • Thanks for your answer. I very much prefer the first option, as I need it to scale to a big amount of data. Fixed widths approach is a bit worrying to me, because I will tie my app to a particular screen resolution, and there are a lot of android phones with different resolutions. But I guess I can implement some code to calculate width of each column at runtime by iterating over the data.. – SimpleMan Dec 12 '11 at 17:43
  • You don't need to do fixed pixel widths, you can do "fixed" percentage widths by using a `LinearLayout` and setting weights to simulate columns. – kabuko Dec 12 '11 at 17:51
  • I was actually setting weghts for items in LinearLayout, but the items were still misaligned. – SimpleMan Dec 12 '11 at 18:11