1

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.

Nikitha
  • 49
  • 5
  • Sounds like a good use case for `LinearLayout` if you ask me – Ivan Wooll Jan 04 '22 at 08:20
  • Need to achieve it in Constraint layout only. Is that possible? @IvanWooll – Nikitha Jan 04 '22 at 08:29
  • You could wrap the 3 buttons in a ConstraintLayout? – Ivan Wooll Jan 04 '22 at 09:20
  • There is no clear way to make the margin on the third widget depends upon the visibility of two other widgets just by setting a gone margin. You can programmatically change the margin of the third widget when the visibility of the first two is "gone". If you must use a gone margin, there is a complicated way to do it with a barrier and a zero width/height widget and some coding, but is would be better just to change the margin as needed in code. – Cheticamp Jan 04 '22 at 13:50

1 Answers1

1

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

Narendra_Nath
  • 4,578
  • 3
  • 13
  • 31