yaromchikV
yaromchikV

Reputation: 99

How do I pin a View from a NestedScrollView at the bottom of the screen?

I have a RecyclewView and a button below it, it's all inside the NestedScrollView. If I add more items to the RecyclerView, the button will hide.

Is there a way to attach the button to the bottom of the screen if there are a lot of elements, and to the bottom of the RecyclerView if it fits on the screen?

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:itemCount="3"
                tools:listitem="@layout/form_field" />

            <Button
                android:id="@+id/send_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Click me" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

It should be like this:

Upvotes: 1

Views: 2078

Answers (3)

Rohit Kumar
Rohit Kumar

Reputation: 116

You can take that view out of the NestedScrollView. like this:

Here is the edited code, it will work perfectly.

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

    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:itemCount="3"
                tools:listitem="@layout/form_field" />

        </LinearLayout>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:layout_marginBottom="10dp"
        android:id="@+id/send_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Click me" />

</LinearLayout>

Upvotes: 3

praveen singh
praveen singh

Reputation: 1

<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:fillViewport="true" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginTop="15px" >

    <!-- bunch of components here -->

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/spinner"
        android:layout_marginTop="5px"
        android:gravity="center_horizontal|bottom"
        android:paddingTop="2px" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="20px"
            android:paddingRight="20px"
            android:text="Delete" />
    </LinearLayout>
</RelativeLayout>

Upvotes: 0

marco
marco

Reputation: 136

Try to use a ConstraintLayout instead of LinearLayout. Set the constraint of the button on the left, bottom, top and right of the screen in te XML file.

Upvotes: 0

Related Questions