Issue with recycler view inside constraint layout
B

4

6

I'm trying to create a bottom sheet dialog based on a complex layout. What I have to achieve is a dialog with an header layout, a recycler view with a list of element and a bottom bar with a couple of buttons. Here is my code:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!-- Layout injected inside this linear layout container programmatically -->
        <LinearLayout
            android:id="@+id/header_dialog_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@id/form_dialog_recycler_view"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/form_dialog_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toTopOf="@id/bottom_sheet_container_buttons"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/header_dialog_container" />

        <LinearLayout
            android:id="@+id/bottom_sheet_container_buttons"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:id="@+id/dialog_sheet_button_reset"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_reset"
                android:textColor="@color/dangerColor" />

            <Button
                android:id="@+id/dialog_sheet_button_close"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_close" />

        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</layout>

Unfortunately my recycler view height is always equal to zero. I tried to change android:layout_height="0dp" to android:layout_height="wrap_content" but as expected it goes below the bottom container. Am I missing something?

Bombastic answered 8/12, 2017 at 16:14 Comment(0)
F
11

Try app:layout_constraintHeight_default="wrap" for RecyclerView if it's inside ConstraintLayout.

Frazil answered 3/12, 2020 at 10:54 Comment(0)
B
1

I ended up with a different solution. I changed constraint layout with a Linear Layout. And I set up bottom margins.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/header_dialog_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/form_dialog_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="60dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <LinearLayout
            android:id="@+id/bottom_sheet_container_buttons"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="-60dp"
            android:orientation="horizontal">

            <Button
                android:id="@+id/dialog_sheet_button_reset"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_reset"
                android:textColor="@color/dangerColor" />

            <Button
                android:id="@+id/dialog_sheet_button_close"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_close" />

        </LinearLayout>
    </LinearLayout>
</layout>
Bombastic answered 8/12, 2017 at 20:9 Comment(0)
C
1

Use app:layout_constrainedHeight="true" in your RecyclerView. It will adjust inside constraints and won't go below the bottom container.

Crematory answered 4/6, 2021 at 9:9 Comment(0)
I
0

Try this code:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- Layout injected inside this linear layout container programmatically -->
        <LinearLayout
            android:id="@+id/header_dialog_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintBottom_toTopOf="@id/form_dialog_recycler_view"
            app:layout_constraintTop_toTopOf="parent" />

        <android.support.v7.widget.RecyclerView
            android:id="@+id/form_dialog_recycler_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            app:layout_constraintBottom_toTopOf="@id/bottom_sheet_container_buttons"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/header_dialog_container" />

        <LinearLayout
            android:id="@+id/bottom_sheet_container_buttons"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent">

            <Button
                android:id="@+id/dialog_sheet_button_reset"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_reset"
                android:textColor="@color/dangerColor" />

            <Button
                android:id="@+id/dialog_sheet_button_close"
                style="@style/ActionSheetTitle"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_weight="0.5"
                android:background="@color/bottomBarContainerColor"
                android:text="@string/general_close" />

        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</layout>
Influx answered 8/12, 2017 at 16:44 Comment(1)
Thank you for your answer. Unfortunately It didn't work.Bombastic

© 2022 - 2024 — McMap. All rights reserved.