Solution 1 :
In layout3, instead of
android_layout_width="match_parent"
write:
android_layout_width="0dp"
and it will be placed right next to the top of layout2.
In case you wonder how it has a wide range even with width=0dp
, it is because of these 2 constraints:
app:layout_constraintEnd_toEndOf="parent"
app_layout_constraintStart_toEndOf="@id/layout2"
They pull LinearLayout to 2 opposite sides, and as a result it unwraps.
Problem :
I’ve been trying out for a long time now but I just don’t understand why “layout3” extends to the edge of the root element and not just to the right edge of “layout2” as I wanted it to.
Here is the XML code:
<?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"
xmlns_tools="http://schemas.android.com/tools"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="vertical"
android_layout_marginBottom="50dp"
android_id="@+id/layout1">
<TextView
android_layout_width="224dp"
android_layout_height="159dp"
app_layout_constraintTop_toTopOf="parent"
android_layout_marginTop="184dp"
app_layout_constraintVertical_bias="0.5"
app_layout_constraintStart_toStartOf="parent"
app_layout_constraintEnd_toEndOf="parent"
app_layout_constraintHorizontal_bias="0.5"
android_id="@+id/textview1"/>
<LinearLayout
android_orientation="vertical"
android_layout_width="match_parent"
android_layout_height="wrap_content"
app_layout_constraintTop_toBottomOf="@+id/textview1"
android_layout_marginTop="20dp"
app_layout_constraintStart_toStartOf="@id/layout1"
app_layout_constraintEnd_toEndOf="@id/layout1"
android_layout_marginEnd="500dp"
android_id="@+id/layout2">
<TextView
android_layout_width="224dp"
android_layout_height="159dp"
android_layout_marginTop="20dp"
android_layout_marginBottom="20dp"
android_layout_marginStart="20dp"
android_id="@+id/textview2"/>
</LinearLayout>
<LinearLayout
android_orientation="vertical"
android_layout_width="match_parent"
android_layout_height="100dp"
app_layout_constraintTop_toBottomOf="@+id/textview1"
android_layout_marginTop="20dp"
app_layout_constraintStart_toEndOf="@id/layout2"
app_layout_constraintEnd_toEndOf="@id/layout1"
android_id="@+id/layout3">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>