We all know that placing a ListView or GridView inside ScrollView will make them becoming difficult to scroll and can not be expanded. Android doesn’t provide a built in API to handle this problem so we have to do a little hack by subclassing those two views.
This is hack i found on stackoverflow, from Neil Traft ‘s answer:
ExpandableHeightListView
package net.londatiga.android.widget; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.ListView; import android.content.Context; public class ExpandableHeightListView extends ListView { boolean expanded = false; public ExpandableHeightListView(Context context) { super(context); } public ExpandableHeightListView(Context context, AttributeSet attrs) { super(context, attrs); } public ExpandableHeightListView(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. // But do not use the highest 2 bits of this integer; those are // reserved for the MeasureSpec mode. int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, 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; } }
ExpandableHeightGridView
package net.londatiga.android.widget; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.GridView; import android.content.Context; 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. // But do not use the highest 2 bits of this integer; those are // reserved for the MeasureSpec mode. int expandSpec = MeasureSpec.makeMeasureSpec( Integer.MAX_VALUE >> 2, 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; } }
How to use:
//ListView ExpandableHeightListView listView = new ExpandableHeightListView(this); listView.setAdapter(adapter); listView.setExpanded(true); //GridView ExpandableHeightGridView gridView = new ExpandableHeightGridView(this); gridView.setNumColumns(4); gridView.setAdapter(adapter); gridView.setExpanded(true);
As you can see, to make the ListView or GridView expandable, just call the setExapanded(true).
[ad#ad-720×90]







This approach works for GridView inside a ScrollView. Thanks for the workaround!
Hello,
very nice video!
i want to make exact same as shown in your video.
Can you provide some resources that can help me do it.
I use web service to fetch images and want to display them like google play , one big in 1st row, 2 smaller in 2nd row and so..
I bit confused with the appropriate layout files
Thanks in advance
hello i am new in android development and i placed this code
ExpandableHeightListView listView = new ExpandableHeightListView(this);
listView.setAdapter(adapter);
listView.setExpanded(true);
inside oncreate method but its not work for me please help me and provide full code of this example
Very Good tutorial Thanks a lot bro. Can you also add the source code in this tutorial too ?
Does this work also on ExpandableListView?
Thank you, It helped me so much.
I m facing the problem in this when i implement onitemclick listner. i mean its not working which u given example
can u plz tell me how to do that?
Thnak You Very Much.
membantu banget 🙂
Please provide me download link of source code. I need source code.
Thanking You,
Archish Thakkar
Please provide download link of the following code.I need source code.
Hi, thanks for the post.
When I add many items to the ExpandableHeightGridView the scrollbar is automatically scrolled down.
Anyway to avoid this?
Thank you , that was very useful.
I have some checkbox in custom grid view , But it is not showing full content last row is not visible , could you please help?
Oh my god!
Your post save much my time, whenever I don’t know about it even a little bit.
Thank you very much!