6

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?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Torstein I. Bø
  • 1,359
  • 4
  • 14
  • 33

5 Answers5

7

You can use layout_weight in conjuntion with layout_width of 0 like this in your item layout xml:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"  
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  

    <TextView  
         android:id="@+id/text1" 
         android:layout_width="0dp"  
         android:layout_height="wrap_content" 
         android:layout_weight="1"/>  

    <TextView  
         android:id="@+id/text2" 
         android:layout_width="0dp"  
         android:layout_height="wrap_content" 
         android:layout_weight="2"/>
</LinearLayout>

Essentially the layout_width of 0dp forces all the text views to have no width. Since spare width in the linear layout is dished out according the layout_weight after the width of the text views has been determined you start with an even playing field among the text boxes who all have the same width (0). Without the 0 width, the text boxes start off at different widths and adding extra space (via the layout_weight) still leaves the text boxes different width.

Chris Knight
  • 24,333
  • 24
  • 88
  • 134
3

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.

Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
2

You could do one of these instead of wrap_content on the TextViews:

android:layout_width="40dip"

the above sets the density-independent width, or

android:layout_weight="2"

which you can use to set the percentage width on each text view.

Also see this post and the link: What is the difference between "px", "dp", "dip" and "sp" on Android?

Community
  • 1
  • 1
eonor
  • 37
  • 5
1

If you have lots of data to display, do not use TableLayout. The performance and the memory consumption is horrendous. It could take many seconds for your data to be displayed and you will get the Choreographer error that your app is doing too much processing in the UI thread

1

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/