I need to find the height of android ListView with custom ListVAdapter. Each of ListView items can be of varying height. I have tried the following code which I found here:
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
But it doesn't give actual height; it gives same height for all list items. Is it possible to find ListView height with list items of varying height?
ListView
's height? – FlintlockListView
's height so you can assign that to theListView
and show all its rows. If this is what you're trying to do, then you could simply "replicate" theListView
by inflating its row layout for all the items in the adapter(instead of getting its complete height). In the end you shouldn't do this because you'll ignore the purpose of theListView
, to recycle views for performance and you can get in trouble. – FlintlocklistItem.getMeasuredHeight()
. – Improvise