Skip to content

Instantly share code, notes, and snippets.

@RichardSilveira
Created February 19, 2017 13:20
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 RichardSilveira/c34a1c18a61de8656178a5e649476137 to your computer and use it in GitHub Desktop.
Save RichardSilveira/c34a1c18a61de8656178a5e649476137 to your computer and use it in GitHub Desktop.
retrieving total height for listView
public static void getTotalHeightofListView(ListView listView) {
ListAdapter mAdapter = listView.getAdapter();
int totalHeight = 0;
int listWidth = listView.getMeasuredWidth();
for (int i = 0; i < mAdapter.getCount(); i++) {
View mView = mAdapter.getView(i, null, listView);
mView.measure(
View.MeasureSpec.makeMeasureSpec(listWidth, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(0, View.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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment