How to display images in a grid on a card view?

Viewed 161

I am trying to display images on a card view as shown in the image below:

enter image description here

Here's my code, but on running I can simply see the card view, the images and text view don't display.

  <androidx.cardview.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardCornerRadius="8dp"
                app:cardUseCompatPadding="true"
                android:layout_gravity="center_horizontal"
                app:cardBackgroundColor="@color/white"
                app:cardElevation="5dp"
                >
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginTop="10dp"
                        android:fontFamily="@font/allerta"
                        android:text="Search by Speciality"
                        android:textSize="16sp"
                        android:textColor="@color/icontintblack" />

                    <GridLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="20dp"
                        android:columnCount="3"
                        android:rowCount="6">

                            <RelativeLayout
                                android:id="@+id/rlDermatologist"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="fill"
                                android:layout_columnWeight="1"
                                android:layout_rowWeight="1">

                                <de.hdodenhof.circleimageview.CircleImageView
                                    android:id="@+id/imgDerm"
                                    android:layout_width="60dp"
                                    android:layout_height="60dp"
                                    android:src="@drawable/ic_derma"
                                    app:civ_border_width="1dp"
                                    app:civ_border_color="@color/e_cure_blue"
                                    android:layout_centerHorizontal="true"/>

                                <TextView
                                    android:layout_below="@+id/imgDerm"
                                    android:layout_centerHorizontal="true"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:textSize="16sp"
                                    android:text="Skin\nSpecialist"
                                    android:textAlignment="center"/>

                            </RelativeLayout>
                    </GridLayout>
</androidx.cardview.widget.CardView>

I tried implementing the grid items (image and textview) with linear layout and they were visible, but with linear layout the design isn't responsive and the image and textview don't get center aligned properly. I am new to android development.

1 Answers

For achieving this type of layout, You can use recyclerview with GridLayoutmanager or StaggerLayoutManager

If You are using GridLayout then you need to add one view as repeated in GridLayout. For info regarding GridLayout, Check this link

Related