Search code examples
androidgrid-layoutandroid-gridlayout

android gridlayout not showing weighted items in apis under 23


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>

Solution

  • 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