Home > Software engineering >  Nested ConstraintLayouts not showing
Nested ConstraintLayouts not showing

Time:11-14

I have two ConstraintLayouts split in half like this:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        style="@style/Widget.Material3.CardView.Elevated"
        android:layout_width="match_parent"
        android:layout_height="96dp"
        app:cardCornerRadius="4dp"
        app:cardElevation="5dp"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@ id/start_viewHolder"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                app:layout_constraintEnd_toStartOf="@id/end_viewHolder"
                app:layout_constraintStart_toStartOf="parent">

                <ImageView
                    android:id="@ id/date_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_round_schedule"
                    app:layout_constraintBottom_toTopOf="@id/amount_key"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@ id/date_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/date_key" />

                <TextView
                    android:id="@ id/amount_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/amount"
                    app:layout_constraintBottom_toTopOf="@id/id_key"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/date_key" />

                <TextView
                    android:id="@ id/amount_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/amount_key" />

                <TextView
                    android:id="@ id/id_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="شناسه تراکنش"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/amount_key" />

                <TextView
                    android:id="@ id/id_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/id_key" />

            </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@ id/end_viewHolder"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toEndOf="@id/start_viewHolder">

                <ImageView
                    android:id="@ id/time_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_outline_calendar_month"
                    app:layout_constraintBottom_toTopOf="@id/status_key"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <TextView
                    android:id="@ id/time_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/time_key" />

                <TextView
                    android:id="@ id/status_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/status"
                    app:layout_constraintBottom_toTopOf="@id/transactionCode_key"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/time_key" />

                <TextView
                    android:id="@ id/status_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/status_key" />

                <TextView
                    android:id="@ id/transactionCode_key"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/transaction_code"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/status_key" />

                <TextView
                    android:id="@ id/transactionCode_value"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="@id/transactionCode_key" />

            </androidx.constraintlayout.widget.ConstraintLayout>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

The problem is that the two child ConstraintLayouts start_viewHolder and end_viewHolder do not show up on Device/Emulator but work correctly on design tab's preview! How can I fix this issue?

CodePudding user response:

Wherever you have android:layout_width="0dp" you must constrain the left and right side of the view, otherwise the view has zero width. That is the same with a 0dp height except the top and bottom must be constrained.

I would also remove the match_parent width/height from any direct child of a ConstraintLayout and replace it with 0dp with the appropriate left/right or top/bottom constraints. match_parent is discouraged with direct child views of ConstraintLayout.

CodePudding user response:

The layout has no problems. The issue came from where I wanted to inflate this layout and hadn't specified the parent layout:

val binding = ReceiptItemViewBinding.inflate(inflater)

To have the layout respect the parent's constraints, it should be implemented with its parent specified:

val binding = ReceiptItemViewBinding.inflate(inflater, parent, false)
  • Related