3

I am trying to populate a GridView with 6 imagebuttons, all of which have width and height set to 0dp, and are weighted on both row and column

While coding in Android Studio the designer was giving the desired results, as pictured

Desired Result

However when it came to running the code on a device, none of the image buttons showed up. After a lot of head-scratching and googling, I tried changing to renering API in Android Studio and setting it to anything below 23 gave the same behavior - none of the items showing up in my grid view.

Here is my code..

    <GridLayout
        android:layout_width="0dp"
        android:background="#ff0000"
        android:stretchMode="columnWidth"
        android:rowCount="2"
        android:columnCount="3"
        android:layout_weight="1"
        android:layout_height="match_parent">


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:padding="0dp"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:adjustViewBounds="true"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitCenter"
            android:src="@drawable/ic_launcher"/>


    </GridLayout>
LairdPleng
  • 948
  • 3
  • 9
  • 28
  • DId you manage to find a solution for that problem? I'm struggling exactly with the same issue right now! – Anorflame Feb 20 '16 at 10:36
  • @Anorflame If I recall correctly, I discovered that weighted GridLayouts only work from Marshmallow onwards. I ended up just changing the layout to a pair of Linear Layouts (one for each row) to deal with my particular case – LairdPleng Feb 25 '16 at 11:40
  • I see... I ended up using the same solution. Well, thanks anyway! – Anorflame Feb 27 '16 at 08:41

1 Answers1

3

I had exactly the same problem but was able to resolve it using android.support.v7.widget.GridLayout and app:layout_columnWeight="1" (instead of just 'GridLayout' and 'android:layout_columnWeight'

For more details see my response to this similar question: android gridlayout not showing in emulator API 22

Slartibartfast
  • 591
  • 5
  • 18