Android NestedScrollView Using Kotlin With Example

Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.

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

Output

Tutorialwing Kotlin NestedScrollView Output Android NestedScrollView Using Kotlin

Tutorialwing Kotlin NestedScrollView Output

Getting Started

Android NestedScrollView can be defined as below –

NestedScrollView is a widget that are used when we need to implement scrollable view inside another scrollable view.

Generally it is difficult to implement scrollable view inside another scrollable view because it would be difficult to decide, by system, which view to scroll. That’s where nestedScrollView comes in.

Different Attributes of Android NestedScrollView Widget

Attributes of NestedScrollView as same as scrollView.




Example of Android NestedScrollView Using Kotlin

At first, we will create android application. Then, we will use nestedScrollView using kotlin in the application.

1. Creating New Project in Kotlin

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

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as NestedScrollView. Then, check Include Kotlin Support and 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. You will get a newly created project successfully if you have followed steps properly.

Since we have a project now, we will modify xml and other files to use nestedScrollView using kotlin 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 some images, stored in res/drawable folder, to be used in the application. These drawable images will be used to create views inside scrollView in the application.

4. Use NestedScrollView Widget in xml file

Open src/main/res/layout/activity_main.xml file and 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 two scrollable views. They are –
a. Scrollable view inside scrollView. They include imageViews and nestedScrollView.
b. Scrollable view inside nestedScrollView. They include textView inside linearLayout. Note that we have defined only
one direct child of nestedScrollView because it can contain only one direct child.

5. Access NestedScrollView Widget in Kotlin file

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

package com.tutorialwing.nestedscrollview

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

We have modified nothing in MainActivity.kt file. 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 file

Code inside 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 our tutorial on Android NestedScrollView using Kotlin.

Support Us

If you like Tutorialwing and would like to contribute, you can email an article on any educational topic at tutorialwing@gmail.com. We would love to publish your article. See your article on Tutorialwing and help others with your knowledge. Follow Facebook, LinkedIn, Google+, Twitter, Youtube for latest updates.
Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners