app:layout_constraintBottom_toBottomOf="parent" for inflated XML layout

Viewed 7949

enter image description here

I have inflated an XML layout over the top of a Views canvas. The buttons "Button to Bottom" I would expect that they should be snapped to the bottom of the screen however the app:layout_constraintBottom_toBottomOf="parent" is not working.

How can I get the bottom Linear Layout to snap to the bottom of the screen and the top LinearLayout to snap to the top of the screen?

Here is the xml:

                <?xml version="1.0" encoding="utf-8"?>
            <android.support.constraint.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:id="@+id/constlayout_matchreplay_actionbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:visibility="visible">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <Button
                        android:id="@+id/button_matchreplay_back"
                        android:layout_width="150dip"
                        android:layout_height="50dip"
                        android:text="back" />

                    <Spinner
                        android:id="@+id/spinner_matchreplay_matchselect"
                        android:layout_width="200dip"
                        android:layout_height="45dip"
                        android:background="@android:drawable/btn_dropdown"
                        android:spinnerMode="dropdown" />

                    <Button
                        android:id="@+id/button_matchreplay_forward"
                        android:layout_width="150dip"
                        android:layout_height="50dip"
                        android:text="forward" />

                    <Button
                        android:id="@+id/button_matchreplay_empty_room"
                        android:layout_width="75dip"
                        android:layout_height="50dip"
                        android:text="DELETE"
                        android:textSize="8dip" />

                    <Button
                        android:id="@+id/button_matchreplay_empty_all"
                        android:layout_width="75dip"
                        android:layout_height="50dip"
                        android:text="EMPTY-ALL"
                        android:textSize="8dip" />


                </LinearLayout>

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="8dp"
                    android:orientation="horizontal"
                    android:visibility="visible"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent">

                    <Button
                        android:id="@+id/button4"
                        android:layout_width="150dip"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button to Bottom" />

                    <Button
                        android:id="@+id/button5"
                        android:layout_width="150dip"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="Button to Bottom" />
                </LinearLayout>




            </android.support.constraint.ConstraintLayout>
2 Answers

It is advisable not to use LinearLayout inside ConstraintLayout as ConstraintLayout allows you to create large and complex layouts with a flat view hierarchy (no nested view groups).

Please try below approach

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">


    <Button
        android:id="@+id/button_matchreplay_back"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_marginTop="5dp"
        android:text="back"
        app:layout_constraintEnd_toStartOf="@+id/spinner_matchreplay_matchselect"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Spinner
        android:id="@+id/spinner_matchreplay_matchselect"
        android:layout_width="200dip"
        android:layout_height="45dip"
        android:layout_marginStart="10dp"
        android:layout_marginTop="5dp"
        android:background="@android:drawable/btn_dropdown"
        android:spinnerMode="dropdown"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button_matchreplay_back"
        app:layout_constraintTop_toTopOf="parent" />


    <Button
        android:id="@+id/button_matchreplay_forward"
        android:layout_width="150dip"
        android:layout_height="50dip"
        android:layout_marginTop="15dp"
        android:text="forward"
        app:layout_constraintEnd_toStartOf="@+id/button_matchreplay_empty_all"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button_matchreplay_back" />

    <Button
        android:id="@+id/button_matchreplay_empty_all"
        android:layout_width="75dip"
        android:layout_height="50dip"
        android:layout_marginStart="10dp"
        android:layout_marginTop="15dp"
        android:text="EMPTY-ALL"
        android:textSize="8dip"
        app:layout_constraintEnd_toStartOf="@+id/button_matchreplay_empty_room"
        app:layout_constraintStart_toEndOf="@+id/button_matchreplay_forward"
        app:layout_constraintTop_toBottomOf="@+id/button_matchreplay_back" />

    <Button
        android:id="@+id/button_matchreplay_empty_room"
        android:layout_width="75dip"
        android:layout_height="50dip"
        android:layout_marginStart="10dp"
        android:layout_marginTop="15dp"
        android:text="DELETE"
        android:textSize="8dip"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button_matchreplay_empty_all"
        app:layout_constraintTop_toBottomOf="@+id/button_matchreplay_back" />


    <Button
        android:id="@+id/button5"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:text="Button to Bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/button4"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button4"
        android:layout_width="150dip"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginBottom="5dp"
        android:text="Button to Bottom"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button5" />


</android.support.constraint.ConstraintLayout>

Please see below final screenshot of above xml layout

Constraint layout For more information please see ConstraintLayout

Use this code; Hopefully it will work fine.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/constlayout_matchreplay_actionbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="visible">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button_matchreplay_back"
            android:layout_width="150dip"
            android:layout_height="50dip"
            android:text="back" />

        <Spinner
            android:id="@+id/spinner_matchreplay_matchselect"
            android:layout_width="200dip"
            android:layout_height="45dip"
            android:background="@android:drawable/btn_dropdown"
            android:spinnerMode="dropdown" />

        <Button
            android:id="@+id/button_matchreplay_forward"
            android:layout_width="150dip"
            android:layout_height="50dip"
            android:text="forward" />

        <Button
            android:id="@+id/button_matchreplay_empty_room"
            android:layout_width="75dip"
            android:layout_height="50dip"
            android:text="DELETE"
            android:textSize="8dip" />

        <Button
            android:id="@+id/button_matchreplay_empty_all"
            android:layout_width="75dip"
            android:layout_height="50dip"
            android:text="EMPTY-ALL"
            android:textSize="8dip" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <Button
            android:id="@+id/button4"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button to Bottom" />

        <Button
            android:id="@+id/button5"
            android:layout_width="150dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button to Bottom" />
    </LinearLayout>

</android.support.constraint.ConstraintLayout>
Related