NestedScrollView in Android with Example
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. It is enabled by default. NestedScrollView is used when there is a need for a scrolling view inside another scrolling view. You have seen this in many apps for example when we open a pdf file and when we reached the end of the PDF there is an Ad below the pdf file. This is where NestedScrollView comes in. Normally this would be difficult to accomplish since the system would be unable to decide which view to scroll. Let’s discuss a NestedScrollView in Android by taking an example.
Step 1: Creating a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that choose Java as language though we are going to implement this project in Java language.
Step 2: Before going to the coding section first do some pre-task
- Go to the app -> res -> values -> strings.xml and add two random text strings inside the strings.xml file to display those strings in the activity_main.xml file.
Step 3: Designing the UI
In the activity_main.xml file add the NestedScrollView and inside NestedScrollView add a LinearLayout and inside LinearLayout add two TextView to display the strings which are created inside the strings.xml file and one Button between the TextView. Here is the code for the activity_main.xml file. One can add as many views inside the NestedScrollView’s LinearLayout
Step 4: Working with MainActivity.java file
There is nothing to do with the MainActivity.java file, so keep it as it is.
Output: Run on Emulator