Grid Layout in android studio giraffe making 9 columns even though column count given is 3

95 Views Asked by At

I am trying to create tic-tac-toe for the same i have tried using a grid layout for placing coins/X/O. It is taking three coins in a single column as opposed to single coin in a single column Please find below the code of my layout and the picture of its output.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true">

    <androidx.gridlayout.widget.GridLayout
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:columnCount="3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rowCount="3">

        <ImageView
            android:id="@+id/imageView20"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="0"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView21"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="1"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView22"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="2"
            app:layout_row="0" />

        <ImageView
            android:id="@+id/imageView23"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:src="@drawable/red"
            app:layout_column="0"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView24"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginLeft="16dp"
            android:src="@drawable/red"
            app:layout_column="1"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView25"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="2"
            app:layout_row="1" />

        <ImageView
            android:id="@+id/imageView26"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="0"
            app:layout_row="2" />

        <ImageView
            android:id="@+id/imageView27"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="1"
            app:layout_row="2" />

        <ImageView
            android:id="@+id/imageView28"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/red"
            app:layout_column="2"
            app:layout_row="2" />

    </androidx.gridlayout.widget.GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

I tried adding all the coins one by one as soon as i added 4th coin it started in fourth column in 2nd row. while the expectation was to have it in 1st column of 2nd row.

0

There are 0 best solutions below