android – inner recyclerview inside holder cuts cut-ThrowExceptions

Exception or error:

I have the following XML layout –

<?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/shopping_cart_holder_root_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/very_light_grey"
    android:paddingBottom="@dimen/marketplace_15dp"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="7dp"
            android:background="@drawable/shopping_cart_image_border"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <ImageView
                android:id="@+id/activity_checkout_cart_imageView"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:layout_margin="10dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:src="@mipmap/ic_launcher" />

            <TextView
                android:id="@+id/shopping_cart_holder_item_quantity_tv"
                android:layout_width="35dp"
                android:layout_height="25dp"
                android:layout_gravity="end"
                android:alpha="0.6"
                android:background="@color/color_black"
                android:elevation="1dp"
                android:gravity="center"
                android:maxLines="1"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textStyle="bold"
                app:autoSizeMaxTextSize="16sp"
                app:autoSizeMinTextSize="10sp"
                app:autoSizeStepGranularity="2sp"
                app:layout_constraintStart_toEndOf="@+id/activity_checkout_cart_imageView"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="1" />


        </FrameLayout>


        <LinearLayout
            android:id="@+id/shopping_cart_holder_infobox_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="7dp"
            android:layout_marginTop="@dimen/marketplace_15dp"
            android:layout_marginEnd="0dp"
            android:orientation="vertical">

            <TextView
                android:id="@+id/shopping_cart_holder_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ellipsize="end"
                android:fontFamily="@font/noto_sans"
                android:lines="1"
                android:textSize="16sp"
                android:textStyle="bold"
                app:autoSizeMaxTextSize="16sp"
                app:autoSizeMinTextSize="12sp"
                app:autoSizeStepGranularity="2sp"
                tools:text="Unavailable Name" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/marketplace_cart_holder_by" />

                <TextView
                    android:id="@+id/shopping_cart_holder_vendor_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="7dp"
                    android:fontFamily="@font/noto_sans"
                    android:text="@string/activity_shopping_cart_vendor_name" />

            </LinearLayout>


            <TextView
                android:id="@+id/shopping_cart_holder_price"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="@font/noto_sans"
                android:text="@string/marketplace_cart_holder_0_price"
                android:textSize="16sp"
                android:textStyle="bold" />

            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/marketplace_cart_holder_attribute_recyclerview"
                tools:itemCount="2"
                android:orientation="vertical"
                tools:listitem="@layout/marketplace_cart_attribute_holder"
                tools:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/shopping_cart_holder_quantity_viewgroup"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_marginTop="@dimen/marketplace_15dp"
                android:orientation="horizontal"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.5"
                app:layout_constraintStart_toStartOf="parent">

                <TextView
                    android:id="@+id/shopping_cart_holder_qty"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:fontFamily="@font/noto_sans"
                    android:text="@string/activity_shopping_cart_qty"
                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <ProgressBar
                    android:id="@+id/shopping_cart_holder_progress_bar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:visibility="gone"
                    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"
                    tools:visibility="visible" />

                <androidx.constraintlayout.widget.ConstraintLayout
                    android:id="@+id/shopping_cart_holder_quantity_picker_viewgroup"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toStartOf="@+id/shopping_cart_holder_delete"
                    app:layout_constraintHorizontal_bias="0.5"
                    app:layout_constraintStart_toEndOf="@+id/shopping_cart_holder_qty"
                    app:layout_constraintTop_toTopOf="parent">

                    <ImageView
                        android:id="@+id/shopping_cart_holder_imgMinus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/minus"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />

                    <TextView
                        android:id="@+id/shopping_cart_holder_product_quantity_picked_tv"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="24dp"
                        android:text="@string/shopping_cart_holder_0"
                        android:textColor="@android:color/black"
                        android:textSize="24sp"
                        app:layout_constraintBottom_toBottomOf="@id/shopping_cart_holder_imgMinus"
                        app:layout_constraintStart_toEndOf="@+id/shopping_cart_holder_imgMinus"
                        app:layout_constraintTop_toTopOf="@+id/shopping_cart_holder_imgMinus" />

                    <ImageView
                        android:id="@+id/shopping_cart_holder_imgPlus"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="24dp"
                        android:src="@drawable/plus"
                        app:layout_constraintStart_toEndOf="@+id/shopping_cart_holder_product_quantity_picked_tv"
                        app:layout_constraintTop_toTopOf="parent" />


                </androidx.constraintlayout.widget.ConstraintLayout>

                <ImageView
                    android:id="@+id/shopping_cart_holder_delete"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:fontFamily="@font/noto_sans"
                    android:src="@drawable/remove3x"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


            </androidx.constraintlayout.widget.ConstraintLayout>
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

I have inside this holder a ConstraitLayout that is disabled from view in a certain stage in my app.

enter image description here

enter image description here

As you can see, once I disable the ConstraintLayout from sight the inner RecyclerView gets cut in half.

here is the layout of the inner RV –

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@color/very_light_grey"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/marketplace_cart_attribute_holder_attribute_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/marketplace_14dp"
        android:textSize="16sp"
        tools:text="Attribute Name:" />

    <TextView
        android:id="@+id/marketplace_cart_attribute_holder_attribute_value"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/marketplace_14dp"
        android:textSize="16sp"
        tools:text="Value" />

</LinearLayout>

What am I missing the causes the RV to get cut in half that does not happen when I have the bottom layout visible?

How to solve:

Add margin bottom –

android:layout_marginBottom=”15dp”

to your LinearLayout to avoid the issue for a number of different screen sizes:

<LinearLayout
            android:id="@+id/shopping_cart_holder_infobox_viewgroup"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="7dp"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp"
            android:layout_marginEnd="0dp"
            android:orientation="vertical">

###

Solved – I have a redundant marginTop in my LinearLayout.

Leave a Reply

Your email address will not be published. Required fields are marked *