Prithvi Bhola
Prithvi Bhola

Reputation: 3151

Margin below <TextView/> in <ConstraintLayout/> not working

There is a simple layout with TextView and RecyclerView inside a ConstraintLayout.

    <android.support.constraint.ConstraintLayout
        android:id="@+id/clSelectedPatient"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvSelectPatient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lifcare"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_marginBottom="100dp"
            app:layout_constraintBottom_toTopOf="@+id/rvPatients"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/rvPatients"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"/>

    </android.support.constraint.ConstraintLayout>

android:layout_marginBottom="100dp" below TextView is not working.

Upvotes: 3

Views: 2192

Answers (2)

Eugene Brusov
Eugene Brusov

Reputation: 17844

There are a few mistakes in your layout:

  • You use match_parent for width and using match_parent is prohibited for ConstraintLayout subviews.

Build a Responsive UI with ConstraintLayout - Adjust the view size

Note: You cannot use match_parent for any view in a ConstraintLayout. Instead use "match constraints" (0dp)

  • In order to display vertical margins properly, you have to define vertical constraints for TextView and RecyclerView.

Your fixed layout code would look like:

<android.support.constraint.ConstraintLayout
    android:id="@+id/clSelectedPatient"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvSelectPatient"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="100dp"
        android:text="Lifcare"
        android:textSize="18sp"
        android:textStyle="bold"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/rvPatients" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rvPatients"
        android:layout_width="0dp"
        android:layout_height="100dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvSelectPatient"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

And this is how it looks on device:

enter image description here

Upvotes: 4

Jorge E. Hern&#225;ndez
Jorge E. Hern&#225;ndez

Reputation: 2938

Add

app:layout_constraintBottom_toBottomOf="parent"

to your RecyclerView

Upvotes: 1

Related Questions