Mahmoud Mabrouk Fouad
Mahmoud Mabrouk Fouad

Reputation: 33

Make top of view to be before end of other view ConstraintLayout

I want to achieve A UI where I have Two views VIEW1, VIEW2 with these constraints

  1. VIEW1 has height of wrap_content
  2. VIEW2 should it's top to be before end of VIEW1 with 10dp for example.

Final UI i want with Code

Upvotes: 0

Views: 747

Answers (4)

Mahmoud Mabrouk Fouad
Mahmoud Mabrouk Fouad

Reputation: 33

Another solution by using

a view with fixed height that equal to how many dp VIEW2 should start before end of VIEW1.

full code(also has same ending corners to bottom) ::

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/mainContent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/faded_orange"
        android:text="TextView"
        android:translationY="0dp"
        android:textAlignment="center"
        app:layout_constraintTop_toTopOf="@id/space"
        app:layout_constraintBottom_toBottomOf="@id/space2"
        app:layout_constraintStart_toStartOf="@id/space" />

    <Space
        android:id="@+id/space"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="@id/topLayout"
        />

    <Space
        android:id="@+id/space2"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@id/bottomLayout"
        />

    <TextView
        android:id="@+id/topLayout"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@drawable/header_bg"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/bottomLayout"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@drawable/bg_bottom_view"
        android:text="TextView"
        android:textAlignment="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 1

user13294271
user13294271

Reputation:

As you know, setting layout_marginTop="-10dp" will not work too.

You can try setting translationY="-10dp" to VIEW2 then it will work.

Upvotes: 0

Shweta Chauhan
Shweta Chauhan

Reputation: 6981

As I understand you want view same as you added UI Image. This UI can be achieved using Guideline.

enter image description here

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textview9"
        android:layout_width="0dp"
        android:layout_height="@dimen/_100sdp"
        android:background="@color/bg_counter"
        android:text="textview"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.13" />

    <TextView
        android:id="@+id/textview10"
        android:layout_width="@dimen/_80sdp"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:text="textview"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline" />

</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Aman Arora
Aman Arora

Reputation: 134

enter image description here

You can do something like this.

  • A Top View ( View 1 ) with constraints of top, start and end of parent.
  • A Bottom ( View 2 ) with constraints topToBottomOf = View 1.
  • Add a rounded rectangle ( View 3 ) with topToBottomOf = View 1 and height = 10dp or whatever the overlap height you want to have.

Upvotes: 0

Related Questions