Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ListView fixed height item

Got a little problem. I'd like to create an android list view activity with all items in the list having a fixed height.

So, my item layout (thread_item.xml) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="300dip"
        >
    <TextView android:id="@+id/thread_item_title"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="[dummy]"
              android:textSize="20dip"
              android:textStyle="bold"
            />
    <ImageView android:id="@+id/thread_first_image"
               android:layout_below="@id/thread_item_title"
               android:scaleType="centerInside"
               android:maxWidth="100dip"
               android:maxHeight="100dip"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:adjustViewBounds="true" />
    <TextView
            android:id="@+id/thread_item_preview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@id/thread_first_image"
            android:text="[dummy]"
            android:textSize="15dip">
    </TextView>
</RelativeLayout>

I set layout_height of the root element to 300dip and expect all items to have the same height, but they don't. When I run the application it looks like the height having a wrap_content value.

In addition the activity itself looks like this:

public class ThreadListActivity extends ListActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        /*
         Code to get items from the storage.
        */

        setListAdapter(new ThreadItemAdapter(this, R.layout.thread_item, itemsArray)));


        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                /*Start new Activity. Unrelated stuff.*/
            }
        });
    }
}

And adapter I'm using looks like this:

public class ThreadItemAdapter extends ArrayAdapter<ThreadItem> {

    Activity context;
    List<ThreadItem> items;

    public ThreadItemAdapter(Activity context, int textViewResourceId, List<ThreadItem> items) {
        super(context, textViewResourceId, items);
        this.context = context;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inf = this.context.getLayoutInflater();
        View result;
        if (convertView == null) {
            result = inf.inflate(R.layout.thread_item, null);
        } else {
            result = convertView;
        }

        TextView tbTitle = (TextView) result.findViewById(R.id.thread_item_title);
        TextView tbPreview = (TextView) result.findViewById(R.id.thread_item_preview);
        ImageView ivFirstImage = (ImageView) result.findViewById(R.id.thread_first_image);

        ThreadItem item = items.get(position);

        tbTitle.setText(item.getThreadTitle());
        ivFirstImage.setImageBitmap(item.getFirstImage());

        SimpleLeadingMarginSpan span = new SimpleLeadingMarginSpan(item.getFirstImage() != null ? 5 : 0, 115); // TODO: const
        SpannableString previewText = new SpannableString(item.getThreadPreview());
        previewText.setSpan(span, 0, previewText.length(), 0);
        tbPreview.setText(previewText);

        return result;
    }
}

I can't see why all list items still wrap their content and don't stay 300dip in height. They might be both smaller or bigger then 300dip. I'm using android 2.3.3, testing on HTC Evo 3D device and an emulator (both show same result).

Thanks a lot in advance.

UPD:

Thanks to Miguel and Sam. The solution is to set maxHeight to the textView, that makes my list item grow (that would be +id/thread_item_preview) and setting the RelativeLayout's minHeight to 300dip as well, to prevent it from shrinking.

like image 647
Dmitriy Avatar asked Apr 30 '12 17:04

Dmitriy


4 Answers

when inflating for convertView, instead of just

result = inf.inflate(R.layout.thread_item, null);

do

result = inf.inflate(R.layout.thread_item, parent, false);

The method in question is inflater.inflate(int viewId, ViewGroup parent, boolean attachToRoot) -- because you're not honoring the supplied parent (which in this case is the ListView), whatever dimension you supply to the listview item will by default be set to layout_width=fill_parent, layout_height=wrap_content, ignoring the 300dip height you specified in xml. By supplying the parent view and passing false, the inflater will honor the 300dip height, while not attaching it to the root (parent).

like image 78
josephus Avatar answered Nov 07 '22 03:11

josephus


What if you change all the child view heights in the row from wrap_content to match_parent?


From comments

Have you tried the minHeight and maxHeight attributes? For example:

android:minHeight="300dp"

You should also watch Android's Romain Guy discuss efficiency in adapters and getView().

like image 39
Sam Avatar answered Nov 07 '22 04:11

Sam


Try changing this

android:layout_height="300dip"

to

android:minHeight="300dip"

This worked for me using an ExpandableListView, so I suppose it will work for this case.

like image 41
Miguel Botón Avatar answered Nov 07 '22 03:11

Miguel Botón


You can achieve this by specifying the same dimension for Min and Max. This fixed my problem.

<ImageView
    android:id="@+id/appIconImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:adjustViewBounds="true"
    android:maxHeight="50dp"
    android:maxWidth="50dp"
    android:minHeight="50dp"
    android:minWidth="50dp"
    android:src="@drawable/ic_launcher" />

enter image description here

like image 2
Vinit Shandilya Avatar answered Nov 07 '22 02:11

Vinit Shandilya