Android: How to measure total height of ListView

Cristiano picture Cristiano · Feb 23, 2013 · Viewed 33.6k times · Source

I need to measure total height of the ListView but it seems I'm constantly getting wrong values. I'm using this code:

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

    int totalHeight = 0;
    int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(),
            MeasureSpec.AT_MOST);
    Log.w("DESIRED WIDTH", String.valueOf(listAdapter.getCount()));
    for (int i = 0; i < listAdapter.getCount(); i++) {
        View listItem = listAdapter.getView(i, null, listView);
        listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
        totalHeight += listItem.getMeasuredHeight();
        Log.w("HEIGHT"+i, String.valueOf(totalHeight));
    }

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

Problem is that I'm receiving larger height than I should receive. Every list item is measured differently and all items have the same height so it definitely shouldn't do that. Height of each element is at least double of it's real height.

Thank you in advance.

edit1:

I have two ListViews and they both have items with 6-7 EditText fields. I show/hide ListView if user wants to write/delete values in EditText. Everything works well except when I want to show list, it takes a lot of space because method calculated ListView needs a lot of space. Because of that I have a lot of empty space below that ListView. Like three times more empty space that needed.

Answer

Cristiano picture Cristiano · Feb 24, 2013

Finally I've done it! This is the working code which measures ListView height and sets ListView in full size:

public static void getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

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

}