Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After (too much) research, I stumbled on <a href="https://stackoverflow.com/a/4536955/358813">the excellent answer of Neil Traft</a>.</p> <p>Adapting his work for the <code>GridView</code> has been dead easy.</p> <p>ExpandableHeightGridView.java:</p> <pre><code>package com.example; public class ExpandableHeightGridView extends GridView { boolean expanded = false; public ExpandableHeightGridView(Context context) { super(context); } public ExpandableHeightGridView(Context context, AttributeSet attrs) { super(context, attrs); } public ExpandableHeightGridView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public boolean isExpanded() { return expanded; } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // HACK! TAKE THAT ANDROID! if (isExpanded()) { // Calculate entire height by providing a very large height hint. // View.MEASURED_SIZE_MASK represents the largest height possible. int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); ViewGroup.LayoutParams params = getLayoutParams(); params.height = getMeasuredHeight(); } else { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } } public void setExpanded(boolean expanded) { this.expanded = expanded; } } </code></pre> <p>Include it in your layout like this:</p> <pre><code>&lt;com.example.ExpandableHeightGridView android:id="@+id/myId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="2dp" android:isScrollContainer="false" android:numColumns="4" android:stretchMode="columnWidth" android:verticalSpacing="20dp" /&gt; </code></pre> <p>Lastly you just need to ask it to expand:</p> <pre><code>mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId); mAppsGrid.setExpanded(true); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload