Search code examples
androidandroid-layoutandroid-constraintlayoutlayoutmargins

Gone Margin constraints to multiple elements


I have a constraint layout with 3 buttons as shown below.

<androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/img_subscribe"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dp20"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone"
    />
<androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/img_start_over"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dp45"
    app:layout_constraintStart_toEndOf="@id/img_subscribe"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="gone"
    />
<androidx.appcompat.widget.AppCompatImageButton
    android:id="@+id/img_trailer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/dp45"
    app:layout_goneMarginLeft="@dimen/dp20"
    app:layout_constraintStart_toEndOf="@id/img_start_over"
    app:layout_constraintTop_toTopOf="parent"
    android:visibility="visible"
    />

I want to give gone margin for third button only when first two are not visible. But in this current code if the it is taking gone margin when immediate previous button is not visible. How can I make the 3rd button to take the gone margin if and only if the first two are not visible.

Any suggestions appreciated.


Solution

  • This is a great use case for Chains in constraint layout. Chains in constraintlayout

    Instead of using a linear layout chain your buttons together. The 3rd button will take the gone margin as you want it to