I am trying to make a grid of pictures with 2 columns and 3 rows. However my screen shows only one item.I am not sure why.
This is what I'm getting:
And this is what I'm trying to achieve:
This is my code so far. I was following a tutorial on YouTube and I have rechecked multiple times. I'm still not sure what's wrong:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context=".Interests">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="49dp"
android:layout_weight="2">
<TextView
android:id="@+id/textGrid"
style="@style/HeaderStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Select your interests"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</TextView>
</RelativeLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:columnCount="2"
android:rowCount="3"
android:alignmentMode="alignMargins"
android:padding="14dp"
>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="@drawable/ic_walking_with_dog">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_row="0"
android:layout_column="0"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:src="@drawable/ic_walking_with_dog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal">
</ImageView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10sp"
android:text="Dog Walking"
android:textAlignment="center"
android:textColor="#000"
android:textSize="20sp"
android:textStyle="bold">
</TextView>
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
Some help is greatly appreciated. Thanks!
You didn't set the ROW and COLUMN of GridLayout children correctly. Try to set like below:
First Child with 0,0:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="0"
...>
Second Child with 0,1:
<androidx.cardview.widget.CardView
android:layout_row="0"
android:layout_column="1"
...>
Third Child with 1,0:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="0"
...>
Fourth Child with 1,1:
<androidx.cardview.widget.CardView
android:layout_row="1"
android:layout_column="1"
...>