Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic height for ListView row in android

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?

like image 616
Aleks G Avatar asked Mar 01 '12 14:03

Aleks G


2 Answers

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

like image 199
maebe Avatar answered Oct 15 '22 08:10

maebe


I had the sam problem and @maebe's answer didn't work for me. To get it to work, I had to call requestLayout() on my TextView in adapter's getView.

So, in your case, you would have to call requestLayout() for each TextView:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    // Inflate your view etc.

    row.findViewById(R.id.TxtName).requestLayout();
    row.findViewById(R.id.TxtPackage).requestLayout();
    row.findViewById(R.id.TxtSource).requestLayout();
    return row;
}

I tried all sorts of tricks and this is the only one that worked in my case.

like image 22
muscardinus Avatar answered Oct 15 '22 10:10

muscardinus