views:

46

answers:

3

How can I make columns in Android ListView? I have this list item layout xml:

<?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">
    <TextView android:layout_weight=".3" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:id="@+id/hour"
        android:gravity="center" />
    <ImageView android:id="@+id/symbol" android:scaleType="fitCenter"
        android:layout_gravity="center_horizontal" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_weight=".5" />
    <TextView android:layout_weight=".8" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:id="@+id/temperature"
        android:gravity="center" />
    <TextView android:layout_weight="1" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:id="@+id/percipitation"
        android:gravity="center" />
    <TextView android:layout_weight="1" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:id="@+id/wind_speed"
        android:gravity="center" />
    <TextView android:layout_weight="1" android:layout_width="wrap_content"
        android:layout_height="fill_parent" android:id="@+id/wind_direction"
        android:gravity="center" />
</LinearLayout>

The problem is when the f.ex. wind_direction change from "4" to "300", then the columns are not aligned.

The problem

Who can this be made with fixed width of columns and using the whole width independent of devices?

A: 

Consider using a TableLayout instead of a ListView. This is designed to have rows and columns. See Hello TableLayout.

To add rows programatically, you can inflate the TableRow and call addView on the TableLayout.

Mayra
Thanks, that did work!
tibnor
A: 

I'm wrestling with the same issue. I have tried a TableLayout, but that has the drawback it does not have the OnItemClickListener behaviour that the ListView has. I need to be able to to touch one of the rows, have it highlighted, and trigger an activity (eg. to show details).

I am toying with the idea of manually calculating the available width and setting the width of each textview in the listview accordingly. However that is bound to cause me trouble, and I'm wondering how other people have solved this?

markjan
A: 

Incorporate both. Try using TableLayout instead of LinearLayout for the list item wrapper. I think I saw that work somewhere once. Never tried it myself.

http://www.vbsteven.be/blog/using-the-simpleadapter-with-a-listview-in-android/

quaintdesigns
I tried that but it did not work
tibnor