Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 574
LairdPleng Avatar asked Nov 24 '25 08:11

LairdPleng


1 Answers

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

like image 117
Slartibartfast Avatar answered Nov 26 '25 22:11

Slartibartfast



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!