Android – NestedScrollView

Adding a ScrollView inside ScrollView can be difficult. Most of the times it won’t end well. You will end up adding few workarounds and a person maintaining your code will probably hate you for the rest of his life. Fortunately, with NestedScrollView class this becomes much easier.

If you ever tried this then you know that two things happen:

1. Lint will yell at you because you are adding vertically scrolling ScrollView inside another vertically scrolling widget.

2. It won’t work because only one ScrollView will be scrollable.

ScrollView lint issue

Using NestedScrollView

There are few workarounds you could add in order to get this to work but there is no need for that. Android support v4 library has a class called NestedScrollView and it does exactly what the name suggests. It can be used as both parent or child ScrollView.

Here is an example how to add it in your xml:

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

        <android.support.v4.widget.NestedScrollView
                android:layout_width="match_parent"
                android:padding="@dimen/default_padding"
                android:layout_height="@dimen/nested_scroll_view_height">

            <TextView
                    android:text="@string/hello_world"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textStyle="bold"/>

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

        <include layout="@layout/cards_layout"/>

    </LinearLayout>
</ScrollView>

And that’s it. Lint won’t yell at you anymore and both ScrollViews will work. This is how it looks like:


When to use NestedScrollView?

Most of the time you probably do not need two ScrollViews on the same screen, but if you ever need it NestedScrollView is a great solution. This class has a set of useful methods so you can disable nested scrolling, check if your nested scroll view has nested scrolling parent and so on… You can check out the official documentation for more info.

  • Cognos Chen

    nice demo video!

  • Aditya Naik

    Thanks! 🙂 Feeling a bit smarter already.

  • Антон Шкуренко

    Hello, can you tell, how to make horizontal nested scroll with scroll behavior in coordinator layout?

  • Anang

    Thank you so much. All I needed was to replace the root scrollview in all my fragments to android.support.v4.widget.NestedScrollView

  • Solo

    Thank you so much.

  • Артём Иванов

    Hi, can you share code?
    I did the same with my layout, but it didnt work

    • Adam Hurwitz

      I’m in the same boat. I tried both a TextView and EditText views without the nested scroll scrolling.

  • Adam Hurwitz

    Here’s a sample Android app demonstrating the above code in action. https://github.com/AdamSHurwitz/NestedScrolling