I'm trying to display 8 items inside a gridview. Sadly, the gridview height is always too little, so that it only shows the first row, and a little part of the second.
Setting android:layout_height="300dp"
makes it work. wrap_content
and fill_parent
apparently not.
My grid view:
<GridView
android:id="@+id/myId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" />
My items resource:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:minHeight="?android:attr/listPreferredItemHeight" >
<ImageView
android:id="@+id/appItemIcon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_dialog_info"
android:scaleType="center" />
<TextView
android:id="@+id/appItemText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="My long application name"
android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
The issue does not seem related to a lack of vertical space.
What can I do ?
After using the answer from @tacone and making sure it worked, I decided to try shorting down the code. This is my result. PS: It is the equivalent of having the boolean "expanded" in tacones answer always set to true.
After (too much) research, I stumbled on the excellent answer of Neil Traft.
Adapting his work for the
GridView
has been dead easy.ExpandableHeightGridView.java:
Include it in your layout like this:
Lastly you just need to ask it to expand:
Found tacones answer helpfull... so i ported it to C# (Xamarin)
Another similar approach that worked for me, is to calculate the height for one row and then with static data (you may adapt it to paginate) you can calculate how many rows you have and resize the GridView height easily.
Use this code after setting the adapter and when the GridView is drawn or you will get height = 0.
Just calculate the height for AT_MOST and set to on measure. Here GridView Scroll will not work so. Need to use Vertical Scroll View explicitly.