androidandroid-layoutandroid-recyclerviewandroid-gridlayoutnestedrecyclerview

Recycerview dynamic height for grid layout


I want to achieve this type of dynamic height of grid layout in recycler view. If a cell is having two lines then other cells in the same row should be of same height despite of having one line in other cells.

enter image description here

Here is my row file code :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginRight="-4dp"
    android:layout_marginTop="-4dp"
    android:layout_marginBottom="-20dp"
    android:layout_marginLeft="-12dp">

    <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cvHomeItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="@dimen/margin_8"
        app:cardElevation="@dimen/margin_8"
        app:cardUseCompatPadding="true">

        <LinearLayout
            android:id="@+id/llChild"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/_6ssp"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/minus_margin_2"
                android:gravity="center_vertical"
                android:weightSum="1">

                <ImageView
                    android:id="@+id/ivImage"
                    android:layout_width="@dimen/margin_24"
                    android:layout_height="@dimen/margin_24"
                    android:layout_marginLeft="@dimen/margin_2"
                    android:scaleType="centerInside"
                    tools:src="@drawable/ic_announcement"
                    android:contentDescription="@string/app_name" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="end">

                    <TextView
                        android:id="@+id/tvDays"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="@dimen/margin_12"
                        android:gravity="center_vertical"
                        android:lines="2"
                        android:textColor="@color/colorText"
                        android:textSize="@dimen/_11ssp"
                        tools:text="3.5 Day(s)" />
                </LinearLayout>
            </LinearLayout>

            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                tools:text="Annual Leave"
                android:textColor="@color/colorText"
                android:textSize="@dimen/_11ssp" />

        </LinearLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

Do let me know if any clarification or any code required. Thank you!


Solution

  • Make the height of your linearlayout ("@+id/llChild) to match_parent

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="-4dp"
        android:layout_marginTop="-4dp"
        android:layout_marginBottom="-20dp"
        android:layout_marginLeft="-12dp">
    
        <androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/cvHomeItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/white"
            app:cardCornerRadius="@dimen/margin_8"
            app:cardElevation="@dimen/margin_8"
            app:cardUseCompatPadding="true">
    
            <LinearLayout
                android:id="@+id/llChild"
                android:layout_width="match_parent"
                android:layout_height="match_parent" <= correction
                android:layout_margin="@dimen/_6ssp"
                android:gravity="center_vertical"
                android:orientation="vertical">
    
                .....
    
            </LinearLayout>
    
        </androidx.cardview.widget.CardView>
    
    </LinearLayout>