Compaq LE2202x
Compaq LE2202x

Reputation: 2376

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

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?

Upvotes: 4

Views: 7933

Answers (2)

Abu Saad Papa
Abu Saad Papa

Reputation: 1946

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.

Upvotes: 0

elL
elL

Reputation: 777

Try adding this one in your activity.

ExpandableHeightGridView exGridView;

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

I based it from your link.

Upvotes: 2

Related Questions