40

I implement NonSwipeableViewPager with a fragment has NestedScrollView like this, what I expect is that the scrollview can scroll up and show 2 textviews:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include
                android:id="@+id/header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                layout="@layout/header" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="16dp"
                android:src="@drawable/ic_up" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Text 2" />

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

But it could not scroll, I tried many ways but still did not get any solution

Norutan
  • 1,480
  • 2
  • 14
  • 27
  • 2
    ScrollView needs their child's height to be wrap_content – Tim May 13 '16 at 09:24
  • @TimCastelijns so we don't have any ways to do it with match_parent height, do we? – Norutan May 13 '16 at 09:32
  • 1
    that's just how a scrollview works, you can set the scrollview to match parent, but not the child of the scrollview, that needs to be wrap content – Tim May 13 '16 at 09:34
  • in the child, you can set something like: android:layout_height="wrap_content" android:minHeight="50dp" – avisper May 16 '22 at 08:24

6 Answers6

131
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

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

This linearlayout should have android:layout_height="wrap_content".

Reason for that is that if the scrollview's child is the same size as the scrollview itself (both match_parent for height) it means that there is nothing to scroll through, since they are of same size and the scrollview will only be as high as the screen.

If the linearlayout has a height of wrap_content then the height is not related to the height of the screen and the scrollview will be able to scroll through it.

Just remember that a scrollview can only have 1 direct child, and that child needs android:layout_height="wrap_content"

Tim
  • 41,901
  • 18
  • 127
  • 145
  • 16
    For me, only `fillViewPort = true` woked. Child's height didn't matter in my case – Dhruvam Gupta Oct 20 '17 at 13:59
  • Yup same here! Just using `fillViewPort = true` worked. But @Tim has a point about `android:layout_height` which is true at times, faced issues in the past. – sud007 Feb 20 '20 at 11:10
  • FYI `fillViewPort=true` will make it so the scrollview takes up the remaining space on the screen, if the content of the scrollview in itself does not – Tim Oct 22 '20 at 11:58
15

In my case app:layout_behavior="@string/appbar_scrolling_view_behavior" this is working only if some one face problem will be try it and may be solve your problem too. you should add also android:fillViewport="true" but without this my code working.

 <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@drawable/subscription_background"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
Md Tariqul Islam
  • 2,736
  • 1
  • 20
  • 35
5

For me it worked when i added "android:layout_marginBottom="100dp"" for last child in androidx.core.widget.NestedScrollView

  • Not a solution, but I saw this same thing, I'm yet to understand why the scrollview doesn't understand the content is scrollable – Boy Sep 20 '20 at 18:17
  • Had the same problem added "android:layout_marginBottom="200dp" worked for me. – Toby Feb 15 '21 at 14:08
1

In the Nested Scroll View use the constraint layout & set the bottom constraint to parent and the top constraint to the UI component above then the left and right constraints to 0. The content will scroll properly. Try this worked for me. I hope it does for you too.

<androidx.core.widget.NestedScrollView
    android:id="@+id/nestedScrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
    tools:ignore="ExtraText">

And the constraints of the child should be "match parent":

 <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

Final UI looks like this:

 <androidx.core.widget.NestedScrollView
    android:id="@+id/ScrollView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/materialToolbar"
    tools:ignore="ExtraText">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.cardview.widget.CardView
            android:id="@+id/cardView"
            style="?attr/materialCardViewFilledStyle"
            android:layout_width="380dp"
            android:layout_height="220dp"
            app:cardCornerRadius="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:fontFamily="@font/inter_medium"
                    android:text="Best Seller of the week"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toTopOf="@+id/textView"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@+id/imageView3" />

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="159dp"
                    android:layout_height="62dp"
                    android:fontFamily="@font/inter_semibold"
                    android:gravity="left|center"
                    android:text="MJ's Hazelnut Flavour Chocolate"
                    android:textAlignment="inherit"
                    android:textStyle="bold"
                    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
                    app:layout_constraintStart_toStartOf="@+id/textView3"
                    app:layout_constraintTop_toTopOf="@+id/imageView3" />

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/btnMoreInfo"
                    style="@style/Widget.Material3.Button.TextButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:fontFamily="@font/inter_bold"
                    android:text="More Info"
                    app:icon="@drawable/arrow"
                    app:iconGravity="end"
                    app:layout_constraintBottom_toBottomOf="@+id/imageView3"
                    app:layout_constraintStart_toStartOf="@+id/textView"
                    app:layout_constraintTop_toBottomOf="@+id/textView" />

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="144dp"
                    android:layout_height="188dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="16dp"
                    android:layout_marginBottom="8dp"
                    android:scaleType="centerCrop"
                    android:src="@drawable/choco2"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintCircleRadius="20dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>
        </androidx.cardview.widget.CardView>


        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:fontFamily="@font/inter_medium"
            android:text="This week's recommendations"
            android:textSize="18dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/btnSeeAll"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnSeeAll" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSeeAll"
            style="@style/Widget.Material3.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/inter_semibold"
            android:text="See all"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/cardView" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvHome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:nestedScrollingEnabled="false"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnSeeAll"
            tools:listitem="@layout/rv_item_home" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvPromo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/rvHome"
            tools:listitem="@layout/rv_promo" />

        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:fontFamily="@font/inter_medium"
            android:text="A few words from us"
            android:textSize="18dp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="@+id/btnSeeAll2"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/btnSeeAll2"
            app:layout_constraintVertical_bias="0.25" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSeeAll2"
            style="@style/Widget.Material3.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:fontFamily="@font/inter_semibold"
            android:text="See all"
            android:textAppearance="@style/TextAppearance.Material3.LabelLarge"
            android:textStyle="bold"
            app:layout_constraintEnd_toEndOf="@+id/rvPromo"
            app:layout_constraintTop_toBottomOf="@+id/rvPromo" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvAboutUs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="16dp"
            android:orientation="horizontal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/btnSeeAll2"
            tools:listitem="@layout/rv_about_us" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

Component Tree for better reference: Click here to get an idea.......Happy Coding

0

if you have used netedscrollview as follow you have to use android:scrollbars="vertical"

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintTop_toBottomOf="@id/toolbar_updateUserDetails"
    app:layout_constraintBottom_toBottomOf="parent"
    android:fillViewport="true">
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:scrollbars="vertical"
        >
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Rudra Rokaya
  • 637
  • 1
  • 5
  • 9
-3

You have to calculate your other child because last child binding on nestedScrollView. You add margin like child height. It is working.