12

I have problem setting the height of a single row in a ListView. I have already read hundreds of forums and blog posts, but nothing seems to work for me.

I have a ListView with an ArrayAdapter<Order>. Depending on an attributes of the Order object, different Layouts are used, with different heights for each row. However, in the end, all rows have the same height. I suppose it is the height of the layout given to the constructor of the ArrayAdapter. Does anybody know how to control the height of each row separately?

Thanks for your answers, Filip

SamSPICA
  • 1,366
  • 15
  • 31
Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • you need only change height(1) or you want different layouts(2) ... for (1) ovveride getView and change height there ... for (2) u need also override getItemViewType, getViewTypeCount and getView ... anyway show us your code of overriden getView – Selvin Sep 22 '11 at 09:52

2 Answers2

21

The trick is in the view in your layout item, you need to set the layout_height to wrap_content and that's it.

I made the screen shot of my listView with items with differents heights.

in case you are beginner, this will give you a starting point :

public static final String[] bzz = new String[] { "1", "2", "3", "4", "5" };
ArrayAdapter<String> addap = new ArrayAdapter<String>(this,
                R.layout.list_item, R.id.text_view, bzz);
lv.setAdapter(addap);

enter image description here

Jonathan F.
  • 2,357
  • 4
  • 26
  • 44
Lukap
  • 31,523
  • 64
  • 157
  • 244
1

I solve this problem by changing my code as follow

  1. Change the list_item layout to LinearLayout
  2. adding android:layout_height="wrap_content" in list_item layout properties
  3. adding android:inputType="textMultiLine" in text view in list_item

hope it works for you too.

ted
  • 13,596
  • 9
  • 65
  • 107