Reputation: 4415
I am trying to create a chain with 2 buttons at the bottom of a ConstraintLayout
using a chain but it is not working. I have removed a couple of views above the buttons for clarity
<?xml version="1.0" encoding="utf-8"?>
<ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginEnd="10dp"
android:text="Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintTop_toBottomOf="@id/guideline" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginStart="4dp"
android:layout_marginEnd="10dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintStart_toEndOf="@id/button1"
app:layout_constraintTop_toBottomOf="@id/guideline" />
</ConstraintLayout>
The buttons do show up at the bottom of the screen, one next to each other but they don't occupy the parent width i.e. each being 50% of the screen. They are wrapping around the text at the side of the screen.
What am I doing wrong?
Update:
I see that if I add something other than 0dp
e.g. 200dp
it expands the views. But I was under the impression that I should be using 0dp
to respect the constraints
Upvotes: 0
Views: 323
Reputation: 17037
Here is an example of implementation spreading two button at the bottom of their parents using ConstraintLayout
:
<?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">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="16dp"
android:text="Button 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/button2"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:text="Button 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>
What's the difference between this and your version. For button1
there is additional rule which indicated where is the start of the button: app:layout_constraintStart_toStartOf="parent"
and accordingly for button2
where is the end: app:layout_constraintEnd_toEndOf="parent"
. Which actually helps the ContraintLayout
properly layout the buttons.
Btw, you don't need to add android:layout_alignParentBottom="true"
to your buttons since the parent is not a RelativeLayout
.
Upvotes: 2