Android NestedScrollView Tutorial With Example

Hello Readers! In this post, we are going to learn about how to use android nestedScrollView widget in any android application. We will also go through different attributes of nestedScrollView widget that can be used to customise it.

Output

Tutorialwing Android NestedScrollView Output Android NestedScrollView Tutorial With Example

Tutorialwing Android NestedScrollView Output

Getting Started

NestedScrollView widget can be defined as below –

As the Name suggests, NestedScrollView is a widget that are used when we want to implement scrollable view inside another scrollable view.

Normally, it is difficult to implement scrollable view inside another scrollable view because system would be unable to decide which view to scroll. This is where NestedScrollView comes in.

Attributes of Android NestedScrollView Widget

Attributes of nestedScrollView is same as scrollView. It is a support v4 widget that runs on old version of android as well. It is added in api 22.




Example of Android NestedScrollView Widget

At first, we will create android application. Then, we will use nestedScrollView widget in this application.

1. Creating New Project

Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as NestedScrollView. Then, click next button.
3. Select minimum SDK you need. However, we have selected 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

Now, we will modify xml and java file to use nestedScrollView widget in the application.

2. Modify Values folder

Open res/values/strings.xml file. Then, add below code into it.

<resources>
	<string name="app_name">NestedScrollView</string>
	<string name="no_image">No Image</string>
	<string name="nested_scroll_text">Tutorialwing.com presents tutorial on NestedScrollView!
		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. Nested scrolling
		is enabled by default. NestedScrollView is used when there is need for scrolling inside
		another scrolling view. Normally this would be difficult task because system would be
		unable to decide which view to scroll. This is where NestedScrollView comes into role.
	</string>
</resources>

3. Download Drawable Resources Needed

You will need some images, stored in res/drawable folder, to be used in the application. These drawable images will be used by views inside nestedScrollView widget in the application.

4. Use NestedScrollView Widget in xml file

Open res/layout/activity_main.xml file. Then, add below code into it.

<?xml version="1.0" encoding="utf-8"?>
<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:layout_marginBottom="20dp"
		android:gravity="center"
		android:orientation="vertical">

		<android.support.v4.widget.NestedScrollView
			android:layout_width="match_parent"
			android:layout_height="100dp"
			android:layout_margin="20dp"
			android:background="@android:color/white"
			android:padding="10dp">

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

				<TextView
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:text="@string/nested_scroll_text"/>

			</LinearLayout>

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

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/guava"/>

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/jackfruit"/>

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/mix_fruit"/>

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/pomegranate"/>

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/strawberry"/>

		<ImageView
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:layout_marginBottom="20dp"
			android:contentDescription="@string/no_image"
			android:src="@drawable/zespri_kiwi"/>

	</LinearLayout>

</ScrollView>

In activity_main.xml file, we have defined nestedScrollView inside scrollView. Using nestedScrollView, we have defined scrollable view inside another scrollable view. It is used same as scrollView i.e. It can contain only one direct child. Note that we have defined linearLayout inside nestedScrollView as a direct child. Then, we have defined view to be scrolled inside it. All the imageViews have been defined inside scrollView.

5. Access NestedScrollView Widget in java file

Open src/main/java/com.tutorialwing.nestedscrollview/MainActivity.java file. Then, add below code into it.

package com.tutorialwing.nestedscrollview;

import android.support.v7.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);
	}
}

We have changed nothing in MainActivity. So, it is as it was after creating project.

Since AndroidManifest.xml file is very important in any android application, we are also going to see the content inside this file.

AndroidManifest.xml

Code inside src/main/AndroidManifest.xml file is as below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.nestedscrollview"
		  xmlns:android="http://schemas.android.com/apk/res/android">

	<application
		android:allowBackup="true"
		android:icon="@mipmap/ic_launcher"
		android:label="@string/app_name"
		android:roundIcon="@mipmap/ic_launcher_round"
		android:supportsRtl="true"
		android:theme="@style/AppTheme">
		<activity android:name=".MainActivity">
			<intent-filter>
				<action android:name="android.intent.action.MAIN"/>

				<category android:name="android.intent.category.LAUNCHER"/>
			</intent-filter>
		</activity>
	</application>

</manifest>

When we run the program, we will get output as shown above.

That’s end of tutorial on Android NestedScrollView widget.

Leave a Reply