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:


    <LinearLayout android:layout_width="match_parent"




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


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.