Dynamic height for ListView row in android
Asked Answered
P

2

6

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?

Purpure answered 1/3, 2012 at 14:40 Comment(4)
Better way use android:maxLines=1 in Textview in Layout of row its Prevent more then one line textIchthyo
Have you tried adujusting android:layout_weight to 1 or something?Devora
@Samir: I want the exact opposite: the labels MUST wrap, as I need all the text to be visible.Purpure
@AndroSelva: layout_weight on which component? I use to to adjust the width of individual sub-views within the row already, but how can I use it to adjust the height of the row?Purpure
U
8

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

Unbeknown answered 3/3, 2012 at 7:0 Comment(2)
This almost does what I want. That is, the height of the row now extends to allow the wrapping of the labels, however I loose the ability to properly colour columns by setting different background colours to different labels. Now the colour only extends as far down as the label wraps and, if another label wraps to more lines, I get white default background under my coloured label. Any ideas how to address this?Purpure
I'll accept this answer as this is eventually what I had to do - and then go with a whole bunch of other painful processing to get the layout the way I wanted.Purpure
L
5

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.

Lazare answered 26/3, 2013 at 14:23 Comment(1)
+1 for this hack. Made my day. Thanks a lot @LazareHabituate

© 2022 - 2024 — McMap. All rights reserved.