androidandroid-constraintlayout

Constraint layout making dependent view gone the UI is distorted


I have the below UI which is fine till the Live image is visible

Correct UI

Below is the XML layout

<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="@dimen/epg_item_container_width"
android:layout_height="@dimen/epg_item_container_with_description_height"
android:layout_margin="1dp"
android:clickable="true"
android:focusable="true"
android:padding="10dp">

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/program_image"
    android:layout_width="150dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,16:9"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:src="@drawable/epg_info_default_icon" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="5dp"
    app:layout_constraintBottom_toBottomOf="@+id/program_image"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/program_image"
    app:layout_constraintTop_toTopOf="@+id/program_image">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/program_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:ellipsize="end"
        android:ems="2"
        android:focusable="false"
        android:gravity="start|top"
        android:maxLines="2"
        android:textColor="#d3d3d3"
        android:textSize="13sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="2023 Stanley Cup Final" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imgLive"
        android:layout_width="42dp"
        android:layout_height="16dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/epg_live"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/program_title" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/program_description"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="5dp"
        android:singleLine="true"
        android:textColor="#FFFFFF"
        android:textSize="10sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="@+id/imgLive"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imgLive"
        app:layout_constraintTop_toTopOf="@+id/imgLive"
        tools:text="| US-TV-PG" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/synopsis"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:clickable="true"
        android:ellipsize="end"
        android:ems="2"
        android:maxLines="2"
        android:textColor="#d3d3d3"
        android:textSize="13sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toStartOf="@+id/menu"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imgLive"
        tools:text="Extended game replays from the 2023 Stanley Cup Final." />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/menu"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:src="@drawable/epg_info_default_icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

Now if I change the visibility to gone for imgLive UI is distorted and program_description overlaps with program_title

Attached refrence after gone

enter image description here


Solution

  • You can wrap imgLive and programDescription inside a simple LinearLayout, something 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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="@dimen/epg_item_container_width"
        android:layout_height="@dimen/epg_item_container_with_description_height"
        android:layout_margin="1dp"
        android:clickable="true"
        android:focusable="true"
        android:padding="10dp">
    
        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/program_image"
            android:layout_width="150dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="H,16:9"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:src="@drawable/epg_info_default_icon" />
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginStart="5dp"
            app:layout_constraintBottom_toBottomOf="@+id/program_image"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/program_image"
            app:layout_constraintTop_toTopOf="@+id/program_image">
    
            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/program_title"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:ellipsize="end"
                android:ems="2"
                android:focusable="false"
                android:gravity="start|top"
                android:maxLines="2"
                android:textColor="#d3d3d3"
                android:textSize="13sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="2023 Stanley Cup Final" />
    
            <LinearLayout
                android:id="@+id/group"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/program_title"
                app:layout_constraintBottom_toTopOf="@id/synopsis"
                android:orientation="horizontal">
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/imgLive"
                    android:layout_width="42dp"
                    android:layout_height="16dp"
                    android:layout_marginEnd="5dp"
                    android:src="@drawable/epg_live"/>
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/program_description"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:singleLine="true"
                    android:textColor="#FFFFFF"
                    android:textSize="10sp"
                    android:textStyle="bold"
                    tools:text="| US-TV-PG" />
            </LinearLayout>
    
            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/synopsis"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:clickable="true"
                android:ellipsize="end"
                android:ems="2"
                android:maxLines="2"
                android:textColor="#d3d3d3"
                android:textSize="13sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toStartOf="@+id/menu"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/group"
                tools:text="Extended game replays from the 2023 Stanley Cup Final." />
    
            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/menu"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/epg_info_default_icon"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>