7

I am unsure about the best way to specify margins in ConstraintLayout around a Barrier.

I tried setting them in the barrier element, but this has no effect and I also couldn't find any documentation on that.

   <androidx.constraintlayout.widget.Barrier
            android:id="@+id/detail_barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="top"
            android:layout_marginBottom="8dp"
            app:constraint_referenced_ids="detail_header_1,detail_header_2" />
Display name
  • 2,465
  • 1
  • 27
  • 46
  • Add the margin for the element against the barrier, the elements that are gonna be pushed by the barrier – cutiko Oct 28 '19 at 12:29
  • Thanks @cutiko , I already tried that, the problem is that then I need to add the margin to all the elements, I wanted to avoid this redundancy by using a barrier – Display name Oct 28 '19 at 12:49
  • 1
    That is how constraint layout margins work, you can't push away one element with others, but the element setting the constraint to another can distance itself from that – cutiko Oct 28 '19 at 13:02
  • @Displayname If you only need the margin with the `Barrier` so that you can set same margin for all constrained elements, would using `GuideLine` make more sense? This way you could set `app:layout_constraintGuide_end` and `app:layout_constraintGuide_start`. – theThapa Oct 31 '19 at 20:57
  • @theThapa I used the Barrier because it depends on specific views and not just on a margin from the edges, because as far as I understood Guidelines can't be dependant on specific views – Display name Nov 04 '19 at 09:49
  • @Displayname Can you please post full xml view to help picture what you are intending to do? – theThapa Nov 05 '19 at 14:30

2 Answers2

11

Using what azEf recommended works, but you need 2 views instead of 1, and also the barrier will look off at the Layout Preview. The built-in way to do this is app:barrierMargin. Example:

preview of the barrier + barrierMargin usage

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_available"
        app:layout_constraintStart_toStartOf="@id/panelStart" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/iconEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierMargin="10dp"
        android:orientation="vertical"
        app:barrierDirection="end"
        app:constraint_referenced_ids="icon" />

    <TextView
        style="@style/title_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxLines="2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@id/iconEnd"
        tools:text="Title" />
Carsten Hagemann
  • 576
  • 5
  • 20
2

You could constrain a Space view to the barrier and add the margin on it.

<androidx.constraintlayout.widget.Barrier
            android:id="@+id/detail_barrier"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:barrierDirection="top"
            app:constraint_referenced_ids="detail_header_1,detail_header_2" />

<Space
            android:id="@+id/detail_space"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            app:layout_constraintTop_toTopOf="@id/details_barrier" />
Asapha
  • 375
  • 4
  • 10