Skip to content

Instantly share code, notes, and snippets.

@mohsin
Last active August 24, 2018 04:56
Show Gist options
  • Select an option

  • Save mohsin/cc9af86e2113538209b0 to your computer and use it in GitHub Desktop.

Select an option

Save mohsin/cc9af86e2113538209b0 to your computer and use it in GitHub Desktop.
setListViewHeightBasedOnChildren fix for TwoLineListItem
public static void setListViewHeightBasedOnChildren(ListView listView)
{
ListAdapter listAdapter = listView.getAdapter();
if(listAdapter == null) return;
if(listAdapter.getCount() <= 1) return;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
int totalHeight = 0;
View view = null;
for(int i = 0; i < listAdapter.getCount(); i++)
{
view = listAdapter.getView(i, view, listView);
view.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
@Tejpalsb
Copy link

how to use it ?? apply to listview?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment