Home > Software engineering >  RecyclerView and NestedScrollView take only half of the screen
RecyclerView and NestedScrollView take only half of the screen

Time:03-01

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>

CodePudding user response:

Add this line in NestedScrollView

android:fillViewport="true"

CodePudding user response:

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.

  • Related