Search code examples
androidandroid-layoutgrid-layout

GridLayout columns do not appear right


I tried to configure a grid layout with 2 rows and 2 columns. But each element here occupies the entire width of the screen, while the second column is outside the screen. Please suggest what am I doing wrong here.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorTheme"
    android:orientation="vertical">

    <GridLayout
        android:id="@+id/moodsGridLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:columnCount="2"
        android:orientation="vertical"
        android:rowCount="2">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="0"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="0"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_column="1"
            android:layout_row="1"
            app:srcCompat="@mipmap/brewtv_logos" />
    </GridLayout>

</LinearLayout>

Solution

  • Try this:

    for Uniform width column and row in Gridlayout use:

    android:layout_columnWeight="1"    
    android:layout_rowWeight="1"
    

    use this layout:

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/color_white"
        android:orientation="vertical">
    
        <GridLayout
            android:id="@+id/moodsGridLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/textView2"
            android:columnCount="2"
            android:orientation="vertical"
            android:rowCount="2">
    
            <ImageButton
                android:id="@+id/imageButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnWeight="1"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageButton
                android:id="@+id/imageButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_columnWeight="1"
                android:layout_row="0"
                android:layout_rowWeight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageButton
                android:id="@+id/imageButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="0"
                android:layout_columnWeight="1"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:src="@mipmap/ic_launcher" />
    
            <ImageButton
                android:id="@+id/imageButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_column="1"
                android:layout_columnWeight="1"
                android:layout_row="1"
                android:layout_rowWeight="1"
                android:src="@mipmap/ic_launcher" />
        </GridLayout>
    
    </LinearLayout>
    

    Output:

    enter image description here