1

I'm migrating some projects to AndroidX with Kotlin. I'm having some issues with ConstraintLayout, I already know that, according to the documentation:

Negative margins will not be supported in ConstraintLayout. [...]

But I have the following situation:

enter image description here

I need to move up in 5dp the LinearLayout, however I need the height to continue to match the lower limit of the screen. That is, I move up 5dp and increment the height by 5dp.

As it's in the image, I've already tried translateY, but it just moves the entire view (not what I need). Also I can not create a view inside the id#top with height of 5dp and align with constraintTop_toTopOf, since they are part of different groups.

Is there any solution for this case?

Jonny
  • 408
  • 4
  • 12

2 Answers2

1

It's tricky to get views to overlap in ConstraintLayout, but you can do it by adding an invisible view and constraining the overlapping view to the invisible view.

In this case the invisible view's bottom could be constrained to the bottom of the green LinearLayout, with a bottom margin of 5dp. The red LinearLayout can then have its top constrained to the bottom of the invisible view. This should give you 5dp of overlap.

Try copy-pasting the following into your constraint layout

            <LinearLayout
                android:id="@+id/green"
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:orientation="vertical"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:background="@android:color/holo_green_light" />

            <View
                android:id="@+id/dummyView"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="5dp"
                android:visibility="invisible"
                app:layout_constraintBottom_toBottomOf="@id/green"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"/>

            <LinearLayout
                android:id="@+id/red"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginStart="16dp"
                android:layout_marginEnd="16dp"
                android:orientation="vertical"
                app:layout_constraintTop_toBottomOf="@id/dummyView"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                android:background="@android:color/holo_red_light" />

Note that a dimension of "0dp" means "match constraints" when set on a child view of a ConstraintLayout. This is not obvious, but is in fact documented here https://developer.android.com/reference/android/support/constraint/ConstraintLayout

Rasmusob
  • 508
  • 3
  • 13
  • I've tried it, it does not work, I even mentioned it in the text. The groups are not the same, I can not create an invisible view and create a constraint for it. Not even create an invisible view, use translateY (since the constraint does not come with a translate) – Jonny Apr 04 '19 at 16:21
  • I have added a simple example that shows how to do it. It is perhaps not possible to do this using the graphical editor, but you can definately do it by hand-coding the layout xml. – Rasmusob Apr 04 '19 at 16:57
  • I understand now. The trick was right on 0dp + marginBottom, you did it, thanks. – Jonny Apr 04 '19 at 17:53
0

The whole purpose of ConstraintLayout is to have a flat view hierarchy. Therefore, having LinearLayouts nested in defeats the purpose.

I suggest you get rid of the nested LinearLayouts and do everything using constraints.

ArcDexx
  • 453
  • 5
  • 15
  • Using a simple constraint-based hierarchy in complex applications would practically shoot yourself in the foot. Since complex applications need to group several elements into views (Linear / Relative) and manage them as one. In my case, migrating this application would have an absurd amount of constraints. – Jonny Apr 04 '19 at 15:32
  • Would you consider sharing a screenshot of the current state ? I have never ran into a case where ConstraintLayout could not handle the job. – ArcDexx Apr 04 '19 at 16:25
  • I'm simulating shadows in a group of objects - grouped by a View (in my case LinearLayout), so the complexity. The position of the second View needs to be superimposed on a part of this shadow, here the problem. This is all due to a custom shadow, Google's fault. Understand, I love constraints (from iOS). – Jonny Apr 04 '19 at 16:39
  • I have successfully achieved negative margins in `ConstraintLayout` using the `Spacer` widget before. This should help : https://stackoverflow.com/questions/42984909/how-to-achieve-overlap-negative-margin-on-constraint-layout/43016512 – ArcDexx Apr 05 '19 at 12:28