Android: How to measure total height of ListView

34,320

Solution 1

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();

}

Solution 2

Try something like this :

listItem.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

  public void onGlobalLayout() {
    int listItemHeight = listItem.getHeight();
  }
});

If you want the height of the listView then try this :

listView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

  public void onGlobalLayout() {
    int listViewHeight = listView.getHeight();
  }
});

You could also check this : Calculate the size of a list view or how to tell it to fully expand

Share:
34,320

Related videos on Youtube

Cristiano
Author by

Cristiano

Updated on February 25, 2020

Comments

  • Cristiano
    Cristiano about 4 years

    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.

    • RobinHood
      RobinHood about 11 years
    • Cristiano
      Cristiano about 11 years
      Thank you but that doesn't work for me. I think I have this problem because I don't have just one ListView. There is probably some conflict with space coverage.
  • Cristiano
    Cristiano about 11 years
    I don't have so many items :) 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.
  • Thibault D.
    Thibault D. about 11 years
    But ListView doesn't know how you are going to use it so it's written this way. If you want the one of the listview to disappear and release the space on the screen for the other list to fill, use View.setVisibility(View.GONE)
  • Cristiano
    Cristiano about 11 years
    I'm using View.setVisibility(View.GONE) but when I show it again it takes really a lot of space, whole screen of empty space. I'm checking a link which @RobinHood posted. It seems they managed to do it by measuring it differently.
  • Cristiano
    Cristiano about 11 years
    Thank you but that doesn't work for me. I made an edit in first post. I think the problem is with multiple lists.
  • Sky Kelsey
    Sky Kelsey over 10 years
    You should change the name of this method. Beginning it with "get" implies a return value other than void.
  • Sky Kelsey
    Sky Kelsey over 10 years
    In addition, this method is very slow to execute, I recommend running it only when the count of list items changes.
  • njzk2
    njzk2 over 10 years
    You also need to add listView.getPaddingTop() and bottom.
  • Léon Pelletier
    Léon Pelletier about 10 years
    If someone has a list containing items with the same height, then I guess doing list.LayoutParams = new LinearLayout.LayoutParams(fill_parent, thisFixedHeight * Count) is the way to go. No iteration anywhere. The real problem is to have something working with lists of lists of list.
  • Gem
    Gem almost 10 years
    I am working with a chat list view. What i think this way of getting total height would be very inefficient way. There could be so may elements in listview.
  • Fedir Tsapana
    Fedir Tsapana over 9 years
    You also need to add totalHeight += listView.getDividerHeight(); for each list item.
  • arober11
    arober11 about 9 years
    Also assumes you have fixed height listView entries. The width of the containing ViewGroup isn't available in the above code, for it to be able to cope with variable height / layout_height="wrap_content" entries.
  • Sergei S
    Sergei S about 9 years
    It does not work when the cells have different heights
  • Saket
    Saket over 8 years
    For list with variable height items, use this: stackoverflow.com/a/21638850/2511884
  • IntoTheDeep
    IntoTheDeep about 7 years
    This not working