How to achieve overlap/negative margin on Constraint Layout?

75,226

Solution 1

Update ConstraintLayout now supports negative margins with version 2.1.0-alpha2. Simply state

android:layout_marginTop="-25dp"

for a negative 25dp margin. (This will only work if the top of the view is constrained. A margin has no effect in ConstraintLayout if the margin's side is not constrained.)



Clarification: The answer below remains valid, but I want to clarify a couple of things. The original solution will place a view with a de facto negative offset with respect to another view as stated and will appear in the layout as shown.

Another solution is to use the translationY property as suggested by Amir Khorsandi here. I prefer that solution as simpler with one caveat: The translation occurs post-layout, so views that are constrained to the displaced view will not follow the translation.

For example, the following XML displays two TextViews immediately below the image. Each view is constrained top-to-bottom with the view that appears immediately above it.

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:tint="#388E3C"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_action_droid" />

    <TextView
        android:id="@+id/sayName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say my name."
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintEnd_toEndOf="@+id/imageView"
        app:layout_constraintStart_toStartOf="@+id/imageView" />

    <TextView
        android:id="@+id/sayIt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say it."
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@+id/sayName"
        app:layout_constraintStart_toStartOf="@+id/sayName"
        app:layout_constraintTop_toBottomOf="@id/sayName" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now, let's translate the "Say my name" TextView up by 50dp by specifying

android:translationY="-50dp"

This produces the following:

enter image description here

The "Say my name" TextView has shifted up as expected, but the "Say it" TextView has not followed it up as we might expect. This is because the translation occurs post-layout. Although the view moves post-layout, it can still be made clickable in the new position.

So, IMO, go with translationX and translationY for negative margins in ConstraintLayout if the caveat above doesn't affect your layout; otherwise, go with the space widget as outlined below.

Another caveat: As stated by Salam El-Banna in a comment to another answer, translationX will not be a good solution for RTL layouts since the sign of the translation will dictate the direction of the shift (left/right) regardless of the RTL or LTR nature of the layout.


Original answer

Although it doesn't appear that negative margins will be supported in ConstraintLayout, there is a way to accomplish the effect using the tools that are available and supported. Here is an image where the image title is overlapped 22dp from the bottom of the image - effectively a -22dp margin:

enter image description here

This was accomplished by using a Space widget with a bottom margin equal to the offset that you want. The Space widget then has its bottom constrained to the bottom of the ImageView. Now all you need to do is to constrain the top of the TextView with the image title to the bottom of the Space widget. The TextView will be positioned at the bottom of the Space view ignoring the margin that was set.

The following is the XML that accomplishes this effect. I will note that I use Space because it is lightweight and intended for this type of use, but I could have used another type of View and made it invisible. (You will probably need to make adjustments, though.) You could also define a View with zero margins and the height of the inset margin you want, and constrain the top of the TextView to the top of the inset View.

Yet another approach would be to overlay the TextView on top of the ImageView by aligning tops/bottoms/lefts/right and make suitable adjustments to margins/padding. The benefit of the approach demonstrated below is that a negative margin can be created without a lot of computation. That is all to say that there are several ways to approach this.

Update: For a quick discussion and demo of this technique, see the Google Developers Medium blog post.

Negative Margin for ConstraintLayout XML

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@mipmap/ic_launcher" />

    <android.support.v4.widget.Space
        android:id="@+id/marginSpacer"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="22dp"
        app:layout_constraintBottom_toBottomOf="@+id/imageView"
        app:layout_constraintLeft_toLeftOf="@id/imageView"
        app:layout_constraintRight_toRightOf="@id/imageView" />

    <TextView
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Say my name"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/marginSpacer" />
</android.support.constraint.ConstraintLayout>

Solution 2

Another way is using translationX or translationY like this:

  <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:translationX="25dp"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"/>

it will work like android:layout_marginRight="-25dp"

Solution 3

Negative margins have never been officially supported in RelativeLayout. Negative margins will not be supported in ConstraintLayout. [...]

-- Romain Guy on Jun 8, 2016

Follow these two issues:

https://code.google.com/p/android/issues/detail?id=212499 https://code.google.com/p/android/issues/detail?id=234866

Solution 4

This is what I figured out after hours of trying to find a solution.

Let us consider two images, image1 and image2. Image2 is to be placed on top of image1 positioned to the bottom-right side.

Overlapping Views Example image

We can use Space widget for overlapping views.

Constraint the Space widget's four sides with the four sides of the image1 respectively. For this example, constraint the image2's left side with the Space widget's right side and the image2's top side with the Space widget's bottom side. This will tie image2 with the Space widget and since the Space widget is constrained from all the sides, we can define required horizontal or vertical bias which will move image2 as required.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="match_parent"
android:layout_height="match_parent"
tools:context=".Player">
 <ImageView
     android:id="@+id/image1"
     android:layout_width="250dp"
     android:layout_height="167dp"
     android:src="@android:color/holo_green_dark"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent" />
 <Space
     android:id="@+id/space"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:layout_constraintBottom_toBottomOf="@+id/image1"
     app:layout_constraintEnd_toEndOf="@+id/image1"
     app:layout_constraintHorizontal_bias="0.82"
     app:layout_constraintStart_toStartOf="@+id/image1"
     app:layout_constraintTop_toTopOf="@+id/image1"
     app:layout_constraintVertical_bias="0.62" />
 <ImageView
     android:id="@+id/image2"
     android:layout_width="82dp"
     android:layout_height="108dp"
     android:src="@android:color/holo_green_light"
     app:layout_constraintStart_toEndOf="@+id/space"
     app:layout_constraintTop_toBottomOf="@+id/space" />
 </android.support.constraint.ConstraintLayout>

Additionally, to position image2 on the center-bottom of image1, we can constraint image2's left and right sides with Space widget's left and right sides respectively. Similarly, we can place image2 anywhere by changing image2's constraints with Space widget.

Solution 5

I found a way to do it much simpler.

Basically have the ImageView, then on the Text View add top constraint to match the top constraint of the image and just add the margin top of the TextView to match to achieve the -ve margin type behavior.

Share:
75,226

Related videos on Youtube

bond
Author by

bond

Updated on February 18, 2022

Comments

  • bond
    bond over 2 years

    Is it possible to achieve negative margin on constraint layout to achieve overlap? I am trying to have a image centered on the layout and have a Text view such that it overlaps a by x dp. I tried setting negative margin value but no luck. It would be great if there is a way to achieve this.

    • Eugen Pechanec
      Eugen Pechanec about 7 years
      Guideline may help, I haven't tried.
  • Stas  Shusha
    Stas Shusha over 6 years
    unless image height is variable
  • Leo supports Monica Cellio
    Leo supports Monica Cellio almost 6 years
    This adds a little more nesting, BUT it works for animating views out from the top, unlike the other solutions here, which assume that the View will be completely shown. Thanks!
  • illusionJJ
    illusionJJ almost 6 years
    Not working in my case, ImageView is centered on the parent, TextView is overlap above ImageView. Then Space makes the error when App back from background. I think 0dp, 0dp makes some trouble. What about just use Guideline.
  • Bob Liberatore
    Bob Liberatore almost 6 years
    This answer needs a lot more information. Unclear how putting something in a LinearLayout can be used to accomplish overlap.
  • ductran
    ductran almost 6 years
    This way can't work for view has constraints with parent.
  • Gabriel Vasconcelos
    Gabriel Vasconcelos over 5 years
    It would still work. You'd just need to be careful about where you'd like your upper widget to overlay the image, setting the margin from the right or from the bottom could be a better option, or you could use bias, all of which answer the question.
  • goemic
    goemic over 5 years
    Be aware of this, since the actual position of the view stays at the original position (before translation), it will be translated only visually. Thereby, onClick events will be only triggered from within the old position.
  • Henry
    Henry almost 5 years
    @goemic's statement seems to be wrong, since this is not a tween animation but a property animation. (please correct me if I am wrong)
  • kuzdu
    kuzdu almost 5 years
    Why do I need a Space? I tried without space and I've the same result.
  • AndrewBloom
    AndrewBloom over 4 years
    their idea may be that with constraintlayout you don't need negative margin. That may be true if you have all elements on one layout. Sometimes though you want to group logically elements like in a command strip made of buttons, which has its own advantages for reusability and clarity and encapsulation. At that point the group will have a rectangular shape, and it's this limitation on that shape that makes negative margins useful and necessary again.
  • AndrewBloom
    AndrewBloom over 4 years
    Negative margins emulate exactly the same concept of the baseline on text, being text a complex shape within a rectangular container
  • Lucas P.
    Lucas P. about 4 years
    @kuzdu the point here is that the bottom of the space is constrained at the bottom of the imageView, the margin moves the space up, and then the top of the text view is constrained at the bottom of the space which creates this "half overlaying" effect we are trying to achieve. Please post an answer here if you've managed to get it without a space so we can see the constraints you used on your two views.
  • Salam El-Banna
    Salam El-Banna about 4 years
    bad practice as it does not support RTL layouts
  • VanessaF
    VanessaF about 3 years
    The Update does not work android:layout_marginTop="-25dp". It has not effects
  • Cheticamp
    Cheticamp about 3 years
    @VanessaF Are you using ConstraintLayout version 2.1.0-alpha2 or later?
  • VanessaF
    VanessaF about 3 years
    @Cheticamp: I use implementation 'androidx.constraintlayout:constraintlayout:1.1.3'. I tried to change it to the version number you suggested with implementation 'androidx.constraintlayout:constraintlayout:2.1.0' but all of my XML layout files showed many errors after that. This is why I kept my initial version and I'll try to find another way of using negative margins
  • AndyBoy
    AndyBoy over 2 years
    Thanks, this is working for me. If top view is overlapping you just set android:layout_width="0dp" android:layout_height="0dp" For me its working find after spent half day on this issue.
  • VanessaF
    VanessaF over 2 years
    I now use "androidx.constraintlayout:constraintlayout:2.1.3" but still the suggested soluton does not have any effect.