How to assign wrap_content as height to dynamically loaded gridview
android, gridview
Solution
You need to create a new class for example
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);
}
}
also you need to change in your XML
<com.your.project.WrappingGridView
android:id="@+id/gridviewtable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:verticalSpacing="10dp"/>
and finally in your java class you need to chance the object GridView to WrappingGridView
Problem
I am loading gridview dynamically with buttons. So for that I am using scrollview, But if i assign wrap_content as height to gridview all the buttons are not displayed. I dont want to assign any static height for the gridview. This is the code which I am using: ``` <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:paddingLeft="5dip" android:paddingRight="5dip" > <GridView android:id="@+id/gridviewtable" android:layout_width="fill_parent" android:layout_height=wrap_content" android:horizontalSpacing="10dp" android:numColumns="4" android:verticalSpacing="10dp" > </GridView> </LinearLayout> </ScrollView> ```