Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Columns in ListView Android

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?

like image 235
Torstein I. Bø Avatar asked Aug 31 '10 20:08

Torstein I. Bø


People also ask

What is listview in Android listview?

Android ListView is a ViewGroup which is used to display the list of items in multiple rows and contains an adapter which automatically inserts the items into the list. The main purpose of the adapter is to fetch data from an array or database and insert each item that placed into the list for the desired result.

How to use multiple columns in listview?

Listview has only one column. And if you want to use multiple column, you might want to use grid (which has multiple columns). Multi column listview is very common issue in android, you need to search a bit before posting a tiny question.

How to create a listview instead of a textview?

1.Create an Android Project. It contains an xml layout file, remove the textview widget and add a 2 listviews instead of that, one for the title and other for the contents.Youcan do this with a single listview also. 2.Create a new android layout file and make the layout of a single row of the required listview.

How to add custom row layout in listview in Java?

You can easily done it with defining Custom ListView. For defining custom listview, just define a custom row layout file with 4 textview in horizontal manner. Now inflating this layout file inside the custom adapter of listview, for that you need to override getView () method and inflate that row layout file.


1 Answers

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.

like image 177
Chris Knight Avatar answered Sep 17 '22 21:09

Chris Knight