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.

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.

XML



filter_none

edit
close

play_arrow

link
brightness_4
code

<resources>
    <string name="app_name">GFG | NestedScrollView </string>
    <string name="random_text_1">
        Hadoop is a data processing tool used to process large size data over distributed
        commodity hardware. The trend of Big Data Hadoop market is on the boom and it’s
          not showing any kind of deceleration in its growth. Today, industries are capable
          of storing all the data generated at their business at an affordable price just
          because of Hadoop. Hadoop helps the industry to know their customer’s behavior,
          customers buying priorities i.e. what they loved the most, and click patterns,
          etc. Hadoop provides personalized recommendations and personalizes ad targeting
          features. Companies are generating thousands of petabytes of data every day so the
          demand for Big Data professionals is very high. Even after a few years,
          Hadoop will be considered as the must-learn skill for the data-scientist
          and Big Data Technology. Companies are investing big in it and it will become
          an in-demand skill in the future. Hadoop provides personalized recommendations
        and personalizes ad targeting
          features. Companies are generating thousands of petabytes of data every day so the
          demand for Big Data professionals is very high. Even after a few years,
          Hadoop will be considered as the must-learn skill for the data-scientist
          and Big Data Technology. Companies are investing big in it and it will become
          an in-demand skill in the future.
    </string>
    <string name="random_text_2">
          Humans are coming closer to the internet at a very fast rate. It means that the
          volume of data Industries is gathering will increase as time passes because of
          more users. Industry’s are gradually analyzing the need for this useful information
          they are getting from their users. It is for sure that the data always tends to
          an increasing pattern so the company’s are eventually acquiring professionals
          skilled with Big Data Technologies. According to NASSCOM, India’s Big Data
          Market will reach 16 billion USD by 2025 from 2 billion USD. The growth of smart
          devices in India is growing at a very huge rate that will cause growth in the
          Big Data Market. Since Big Data is growing the demand for Big Data professionals
          will be high.  Hadoop provides personalized recommendations and personalizes ad targeting
          features. Companies are generating thousands of petabytes of data every day so the
          demand for Big Data professionals is very high. Even after a few years,
          Hadoop will be considered as the must-learn skill for the data-scientist
          and Big Data Technology. Companies are investing big in it and it will become
          an in-demand skill in the future.
    </string>
  
</resources>

chevron_right


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

XML

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
  
    <!-- Nested Scroll view -->
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
  
        <!-- Linear layout to contain 2 text view and button -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
  
            <!-- showing random text 1 from strings.xml file -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/random_text_1" />
  
            <!-- simple button -->
            <Button
                android:layout_width="match_parent"
                android:layout_height="160dp"
                android:background="@color/colorPrimary"
                android:text="Nested Scroll View "
                android:textColor="#ffffff"
                android:textSize="32dp" />
  
            <!-- showing random text 2 from strings.xml file -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/random_text_2" />
              
        </LinearLayout>
          
    </androidx.core.widget.NestedScrollView>
  
</RelativeLayout>

chevron_right


Step 4: Working with MainActivity.java file

There is nothing to do with the MainActivity.java file, so keep it as it is.

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

chevron_right


Output: Run on Emulator 

Resources:

  • Download Complete Project from Github
  • Download the Apk file

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.