Skip to content

Instantly share code, notes, and snippets.

@paddyzab
Created May 2, 2013 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paddyzab/5501898 to your computer and use it in GitHub Desktop.
Save paddyzab/5501898 to your computer and use it in GitHub Desktop.
ListViewHeight based on Height and count of Children.
protected static void doSetListViewHeightBasedOnChildren(ListView listView, int extraPixels) {
if (null != listView) {
final ListAdapter listAdapter = listView.getAdapter();
if (null != listAdapter) {
int totalHeight = 0, desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.AT_MOST);
for (int i = listAdapter.getCount(); --i >= 0; ) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = extraPixels + totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment