Skip to content

Instantly share code, notes, and snippets.

@nandha-dev
Last active August 29, 2015 14:15
Show Gist options
  • Save nandha-dev/8ada7dc24f1e3ca5138a to your computer and use it in GitHub Desktop.
Save nandha-dev/8ada7dc24f1e3ca5138a to your computer and use it in GitHub Desktop.
setting height for the Listview inside scrollview
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
listView.setVisibility(View.GONE);
return;
}
int listWidth = listView.getMeasuredWidth();
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(
MeasureSpec.makeMeasureSpec(listWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight
+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment