Skip to content

Instantly share code, notes, and snippets.

@Hackforid
Created October 15, 2014 03:34
Show Gist options
  • Save Hackforid/a9276a9981e2e45c9cc9 to your computer and use it in GitHub Desktop.
Save Hackforid/a9276a9981e2e45c9cc9 to your computer and use it in GitHub Desktop.
Get ListView Height
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, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
DLog.i("lv height=" + params.height);
listView.setLayoutParams(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment