Dynamic height for ListView row in android

Aleks G picture Aleks G · Mar 1, 2012 · Viewed 15.7k times · Source

I'm seeing a strange behavior with my ListView, which I can't seem to get around.

I have a ListView, which is populated with some data. Each row contains an image and three text labels. Here's my XML for the row (I'm inflating it in the Adapter's getView method):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="0dip"
     android:paddingBottom="0dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

    <ImageView android:id="@+id/Icon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:padding="2dp"
        android:scaleType="fitCenter"
        android:contentDescription="@string/app_icon"/>

    <TextView android:id="@+id/TxtName"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:textColor="@android:color/black"
         android:background="@color/list_bg1"
         android:layout_weight="2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtPackage"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="2"
         android:textColor="@android:color/black"
         android:background="@color/list_bg2"
         android:padding="2dp"/>

     <TextView android:id="@+id/TxtSource"
         android:scrollHorizontally="false"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android:layout_weight="2"
         android:background="@color/list_bg3"
         android:textColor="@android:color/black"
         android:padding="2dp"/>
</LinearLayout>

Now, whatever goes into the text fields can be of any lenght (from a few characters to 80-100 characters) and I want the labels to just wrap and the height of the row to expand to accommodate the height of the wrapped labels.

Yet, the labels do wrap ok, but the height of the row doesn't expand, therefore the bottom of the labels gets cut off. Here's my getView method:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    SetInfo app = (SetInfo)getItem(position);
    if(app == null)
        return null;

    LinearLayout row = (LinearLayout) (convertView == null
               ? LayoutInflater.from(context).inflate(R.layout.listitem, parent, false)
               : convertView);
    ((TextView)row.findViewById(R.id.TxtName)).setText(app.getName());
    ((TextView)row.findViewById(R.id.TxtPackage)).setText(app.getPackage());
    ((TextView)row.findViewById(R.id.TxtSource)).setText(app.getSource());
    if(app.getIcon() != null)
        ((ImageView)row.findViewById(R.id.Icon)).setImageDrawable(app.getIcon());
    return row;
}

How can I have the rows to be of different height, adjusted automatically to fit all the content?

Answer

maebe picture maebe · Mar 3, 2012

Try changing your TextViews to have android:layout_height="wrap_content".