0

I used this code to get the total heights of listview row items but it did not returns the actual height. Here is the used code

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter(); 
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

For example: I have a listview with 20 rows and each rows height is different from each others suppose that 200,300,500. When I use this above code, it did not returns actual height for me. Also I tried this answer : Android: How to measure total height of ListView But did not works. How can I get rid of this problem. Can anyone explain this solution??

Community
  • 1
  • 1
androidcodehunter
  • 21,567
  • 19
  • 47
  • 70
  • Is there a reason you need the total height? If you have a large list, this method is going to have some bad performance. – Samuel Jul 12 '13 at 14:21
  • Did you try listItem.getHeight()? http://developer.android.com/reference/android/view/View.html#getHeight() – buczek Jul 12 '13 at 14:23
  • When you used the other answer, what went wrong? – Samuel Jul 12 '13 at 14:25
  • @Samuel actually I have to show list of comments inside listview. When row number 15 or more then it does not return actual height but if the row number is 1 to 5 it works perfectly. I have to show comments under a videoview that's why I need this. Is there any other solution? – androidcodehunter Jul 12 '13 at 14:27
  • @Samuel when I have large list then it won't works exactly what I want. – androidcodehunter Jul 12 '13 at 14:29

1 Answers1

6
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();

The core of the function is these three lines, it try to measure each view. The 0 in listItem.measure(0, 0) is equals to MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)

Mostly it will calculate the accurate height of the listview. There is one exception, when the view content is too much and will wrap line, i.e. there are to many lines of text. In such situation, you should specified a accurate widthSpec to measure(). So change listItem.measure(0, 0) to

// try to give a estimated width of listview
int listViewWidth = screenWidth - leftPadding - rightPadding; 
int widthSpec = MeasureSpec.makeMeasureSpec(listViewWidth, MeasureSpec.AT_MOST);
listItem.measure(listViewWidth, 0)

UPDATE about the formula here

int listViewWidth = screenWidth - leftPadding - rightPadding; 

It's just an example to show how you can estimate the width of the width of listview, the formula is based on the fact that width of listview ≈ width of screen. The padding is set by your self, maybe 0 here. This page tells how to get screen width. In general, it's just a sample, and you can write your own formula here.

Community
  • 1
  • 1
faylon
  • 7,360
  • 1
  • 30
  • 28
  • actually you understand my problems. When too many lines of code, it cannot return the actual height. Now my question is how to get screenWidth, leftPadding and rightPadding. Can you share with us some code snippet to get this three things. thanks for your comment. – androidcodehunter Jul 12 '13 at 17:31
  • Hello you helps me a lot. Another question is arises that where I use this int widthSpec value. – androidcodehunter Jul 12 '13 at 18:48
  • Hello I solved this problem. Thanks for your good and nice comment. – androidcodehunter Jul 12 '13 at 18:51