Create an Android NestedScrollView Programmatically in Android

Hello Readers! In this post, we are going to learn how to create and use android nestedScrollView programmatically in any android application. We will also learn to add nestedScrollView in linearLayout programmatically in any application.

Output

Tutorialwing Android Dynamic NestedScrollView Output Android NestedScrollView Programmatically in Android

Tutorialwing Android Dynamic NestedScrollView Output

Getting Started

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

1. Creating New Project

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as DynamicNestedScrollView. 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 android nestedScrollView programmatically.

2. Modify values folder

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

<resources>
	<string name="app_name">DynamicNestedScrollView</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 scrollView in the application.

4. Modify Layout Folder

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

		<LinearLayout
			android:id="@+id/rootContainer"
			android:layout_width="match_parent"
			android:layout_height="wrap_content"
			android:orientation="vertical">

		</LinearLayout>

		<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 two scrollable views. They are –
a. Views inside scrollView. it includes imageViews and a linearLayout, with id rootContainer, that will act as container for nestedScrollView created dynamically in the application.

5. Create Android NestedScrollView Programmatically / Dynamically

Open app/src/main/java/com.tutorialwing.dynamicnestedscrollview/MainActivity.java file and add below code into it.

package com.tutorialwing.dynamicnestedscrollview;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		// Create NestedScrollView Dynamically
		NestedScrollView nestedScrollView = new NestedScrollView(this);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200);
		layoutParams.setMargins(30, 30, 30, 30);
		nestedScrollView.setLayoutParams(layoutParams);
		nestedScrollView.setBackgroundColor(Color.WHITE);

		// Add NestedScrollView to linearLayout having id rootContainer
		LinearLayout layout = findViewById(R.id.rootContainer);
		if (layout != null) {
			layout.addView(nestedScrollView);
		}

		// Created LinearLayout and add it to NestedScrollView
		LinearLayout linearLayout = new LinearLayout(this);
		LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		linearLayout.setOrientation(LinearLayout.VERTICAL);
		linearLayout.setLayoutParams(linearParams);

		nestedScrollView.addView(linearLayout);

		// Created TextView and add it to linearLayout
		TextView textView = new TextView(this);
		LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		textView.setLayoutParams(textParams);
		textView.setText(getString(R.string.nested_scroll_text));
		textView.setPadding(15, 15, 15, 15);

		linearLayout.addView(textView);
	}
}

We have defined android nestedScrollView programmatically in MainActivity.java file. Then, we have added it in linearLayout having id rootContainer. Then, we have set layout params, margins etc. in it. Then, we have created linearLayout as a direct child of nestedScrollView. After that, we have created textView and added it in linearLayout.

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

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below –

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.dynamicnestedscrollview"
		  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 application, we will get output as shown above.

That’s the end of tutorial on Creating Android NestedScrollView Programmatically.

Leave a Reply