Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 270
Prithvi Bhola Avatar asked Nov 24 '25 22:11

Prithvi Bhola


2 Answers

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

like image 63
Eugene Brusov Avatar answered Nov 27 '25 11:11

Eugene Brusov


Add

app:layout_constraintBottom_toBottomOf="parent"

to your RecyclerView

like image 25
Jorge E. Hernández Avatar answered Nov 27 '25 11:11

Jorge E. Hernández



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!