-1

I have a layout where there is an EditText and Button on the bottom of the screen. I want the NestedScrollView along with the RecyclerView to take up the space above the 2 elements. However, they only take a part of it.

Here is my 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"
    tools:context=".HomeActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/etMessage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:context=".HomeActivity">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rvMessages"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:nestedScrollingEnabled="true" />

    </androidx.core.widget.NestedScrollView>

    <EditText
        android:id="@+id/etMessage"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Enter your message"
        android:inputType="text"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btnSend"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/etMessage" />

</androidx.constraintlayout.widget.ConstraintLayout>
Electric Dragon
  • 176
  • 5
  • 17
  • you might face some problems when your recyclerview is endless, like if you are calling any api while scrolling the recyclerview then there will be continuous api calls when the recyclerview is inside nested scroll view. – Mathi Vanan Mar 01 '22 at 13:51

2 Answers2

3

Add this line in NestedScrollView

android:fillViewport="true"
0

It will have the same height as your NestedScrollView has if RecyclerView has necessary amount of items in it. You can calculate necessary height of items. And I aslo don't really think that you need NestedScrollView in your layout 'cause RecyclerView can be scrolled without it.

Marina
  • 317
  • 4
  • 11