Expandable GridView height does not expand to the content's height

Compaq LE2202x picture Compaq LE2202x · Sep 20, 2013 · Viewed 7.7k times · Source

I am using this ExpandableHeightGridView class where the GridView"supposedly" stretches to the content's height since I am placing it inside a ScrollView. This works fine if my GridViewitems are complete but since my GridViewitems are dynamic, it has not the same number of items always, now the problem is if I have fewer GridViewitems, its height doesn't wrap to the content height but has a space of blank row. I don't understand why it allocates that blank row.

Here's the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondActivity" >

<ScrollView
    android:id="@+id/scrollview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:text="@string/hello_world" />

        <com.example.ExpandableHeightGridView
            android:id="@+id/gridview_module"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:horizontalSpacing="20dp"
            android:isScrollContainer="false"
            android:numColumns="3"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />

        <Button
            android:id="@+id/dial_phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:onClick="dialPhone"
            android:text="Dial Phone" />
    </LinearLayout>
</ScrollView>

I am allocating the GridView weight of 1 and the others are 0. Am I getting it right?

Answer

elL picture elL · Sep 23, 2013

Try adding this one in your activity.

ExpandableHeightGridView exGridView;

exGridView = (ExpandableHeightGridView) findViewById(R.id.myId);
exGridView .setExpanded(true);

I based it from your link.