Skip to content

Instantly share code, notes, and snippets.

@runemart
Created March 26, 2014 11:57

Revisions

  1. runemart created this gist Mar 26, 2014.
    29 changes: 29 additions & 0 deletions WrappingGridView.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    // http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/

    public class WrappingGridView extends GridView {

    public WrappingGridView(Context context) {
    super(context);
    }

    public WrappingGridView(Context context, AttributeSet attrs) {
    super(context, attrs);
    }

    public WrappingGridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightSpec = heightMeasureSpec;
    if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
    // The great Android "hackatlon", the love, the magic.
    // The two leftmost bits in the height measure spec have
    // a special meaning, hence we can't use them to describe height.
    heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    }
    super.onMeasure(widthMeasureSpec, heightSpec);
    }

    }