4

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?

Community
  • 1
  • 1
Compaq LE2202x
  • 2,030
  • 9
  • 45
  • 62

2 Answers2

2

Try adding this one in your activity.

ExpandableHeightGridView exGridView;

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

I based it from your link.

Community
  • 1
  • 1
elL
  • 767
  • 2
  • 14
  • 35
  • I already applied `exGridView.setExpanded(true);` which made it stretch, but it stretched to the full height and not the actual height of the `GridView`. For example, my maximum `GridView` row is 4, but if I have only 2 rows it allocates 2 more blank rows. – Compaq LE2202x Sep 23 '13 at 02:45
0

When you want to give the layout_weight then make sure the layout_height is 0dp. Don't set both the layout_height and layout_weight. Hope that helps you.

Also remove this tag android:fillViewport="true" from the scroll view. You are actually asking the scrollview to expand the gridview if it has lesser items by setting fillViewport to true. If you remove this tag then the gridview will wrap to its content and there will be no extra space below the rows.

Abu Saad Papa
  • 1,908
  • 15
  • 20