set GridView Height Based On Children - Android android.widget

Android examples for android.widget:GridView

Description

set GridView Height Based On Children

Demo Code

import java.lang.reflect.Field;

import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;

public class Main {

  public static void setGridViewHeightBasedOnChildren(GridView gridView) {

    ListAdapter listAdapter = gridView.getAdapter();
    if (listAdapter == null) {
      return;//  ww  w . ja v  a  2  s.  c o  m
    }
    int rows;
    int columns = 0;
    int horizontalBorderHeight = 0;
    Class<?> clazz = gridView.getClass();
    try {
      Field column = clazz.getDeclaredField("mRequestedNumColumns");
      column.setAccessible(true);
      columns = (Integer) column.get(gridView);
      Field horizontalSpacing = clazz.getDeclaredField("mRequestedHorizontalSpacing");
      horizontalSpacing.setAccessible(true);
      horizontalBorderHeight = (Integer) horizontalSpacing.get(gridView);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (listAdapter.getCount() % columns > 0) {
      rows = listAdapter.getCount() / columns + 1;
    } else {
      rows = listAdapter.getCount() / columns;
    }
    int totalHeight = 0;
    for (int i = 0; i < rows; i++) {
      View listItem = listAdapter.getView(i, null, gridView);
      listItem.measure(0, 0);
      totalHeight += listItem.getMeasuredHeight();
    }
    ViewGroup.LayoutParams params = gridView.getLayoutParams();
    params.height = totalHeight + horizontalBorderHeight * (rows - 1);// ????????????
    gridView.setLayoutParams(params);
  }

}

Related Tutorials