Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A helper method that enables an Android GridView to be used inside a vertical ScrollView
/* Inspired by */
public static void justifyListViewHeightBasedOnChildren (GridView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
ViewGroup vg = listView;
int totalHeight = 0;
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, vg);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
ViewGroup.LayoutParams par = listView.getLayoutParams();
par.height = totalHeight + (listView.getVerticalSpacing() * adapter.getCount());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.