adjust cell size automatically on recyclerview

28 views Asked by At

I am creating a gridview using recyclerview and this working fine but I do have an issue in the rendering.

I would like each cell to keep the same size and that the size of each cell is

For example, I have 5 column and it looks like the image below:

enter image description here

As you can see there is space between each cell. This is because I set a defined width for my cell.

Below is the xml for the cell:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

  
    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintDimensionRatio="1"
        android:clickable="true">

        <TextView
            android:id="@+id/itemElevation"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:gravity="center"
            android:textColor="@android:color/black"
            app:elevationItem="@{item}"
            tools:text="33"
            tools:background="@android:color/holo_orange_dark"/>

    </androidx.cardview.widget.CardView>

</layout>

and my recyclerview is done like below:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.MainFragment">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
            app:spanCount="5"
            tools:listitem="@layout/item_cell"
            tools:itemCount="25">

        </androidx.recyclerview.widget.RecyclerView>


    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

I am trying to figure out how to have the cell to use evenly the width of the screen so there is no more space.

I am trying to get as below but automatically, so if I move to 6 columns it will keep the square form but the width will be automatically adapted.

enter image description here

Any idea?

0

There are 0 answers