Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaNestedScrollView in Android with Example

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 by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. The code for that has been given in both Java and Kotlin Programming Language for Android.

Step 2: Working with the activity_main.xml File

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




<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 theBig 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>


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




<?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>


Step 3: Working with the MainActivity File

Go to the MainActivity File and refer to the following code. Since there is no change in MainActivity File, So keep it as it is.

Java




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);
    }
}


Kotlin




import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}


Output:

RELATED ARTICLES

Most Popular

Recent Comments