Android NestedScrollView Using Kotlin With Example

In this article, we will learn about android NestedScrollView using Kotlin. We will go through various example that demonstrates how to use different attributes of NestedScrollView. For example,

In this article, we will get answer to questions like –

  • What is NestedScrollView?
  • Why should we consider NestedScrollView while designing ui for any app?
  • What are possibilities using NestedScrollView while designing ui? etc.

Let’s have a quick demo of things we want to cover in this tutorial –

Output

Tutorialwing Kotlin NestedScrollView Output Android NestedScrollView Using Kotlin

Tutorialwing Kotlin NestedScrollView Output

Getting Started

We can define android NestedScrollView widget as below –

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

Now, how do we use NestedScrollView in android application ?

Creating New Project

At first, we will create an application.
So, follow steps below to create any android project in Kotlin –

Step Description
1. Open Android Studio (Ignore if already done).
2. Go to File => New => New Project. This will open a new window. Then, under Phone and Tablet section, select Empty Activity. Then, click Next.
3. In next screen, select project name as NestedScrollView. Then, fill other required details.
4. Then, clicking on Finish button creates new project.

Newbie in Android ?

Some very important concepts (Recommended to learn before you move ahead)

Before we move ahead, we need to setup for viewBinding to access Android NestedScrollView Using Kotlin file without using findViewById() method.

Setup ViewBinding

Add viewBinding true in app/build.gradle file.

 
 android { 
 	// OTHER CODE... 
 	buildFeatures { 
 		viewBinding true 
 	} 
 } 
 

Now, set content in activity using view binding.
Open MainActivity.kt file and write below code in it.

 
 class MainActivity : AppCompatActivity() { 
 	
 	private lateinit var binding: ActivityMainBinding 
 	
 	override fun onCreate(savedInstanceState: Bundle?) { 
 		super.onCreate(savedInstanceState) 
 		binding = ActivityMainBinding.inflate(layoutInflater) 
 		val view = binding.root 
 		setContentView(view) 
 	} 
 } 
 

Now, we can access view in Kotlin file without using findViewById() method.

Using NestedScrollView in Kotlin

Follow steps below to use NestedScrollView in newly created project –

  • We will need 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.
  • 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>
    
  • Open res/layout/activity_main.xml file. Then, add below code in 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">
    
            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_margin="20dp"
                android:background="@android:color/white"
                android:fillViewport="true"
                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>
    
            </androidx.core.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.

  • We can also access it in Kotlin File, MainActivity.kt, as below –

    package com.tutorialwing.nestedscrollview
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import com.tutorialwing.nestedscrollview.databinding.ActivityMainBinding
    
    class MainActivity : AppCompatActivity() {
    
    	private lateinit var binding: ActivityMainBinding
    
    	override fun onCreate(savedInstanceState: Bundle?) {
    		super.onCreate(savedInstanceState)
    		binding = ActivityMainBinding.inflate(layoutInflater)
    		val view = binding.root
    		setContentView(view)
    	}
    }
    

Now, run the application. We will get output as below –

Tutorialwing Kotlin NestedScrollView Output Android NestedScrollView Using Kotlin

Tutorialwing Kotlin NestedScrollView Output

Different Attributes of NestedScrollView in XML

Now, we will see how to use different attributes of Android NestedScrollView using Kotlin to customise it –

Set Id of NestedScrollView

Many a time, we need id of View to access it in kotlin file or create ui relative to that view in xml file. So, we can set id of NestedScrollView using android:id attribute like below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        />

Here, we have set id of NestedScrollView as nestedScrollView_ID using android:id=”” attribute. So, if we need to reference this NestedScrollView, we need to use this id – nestedScrollView_ID.
Learn to Set ID of NestedScrollView Dynamically

Set Width of NestedScrollView

We use android:layout_width=”” attribute to set width of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        />

Width can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value (like 20dp, 30dp etc.).
Learn to Set Width of NestedScrollView Dynamically

Set Height of NestedScrollView

We use android:layout_height=”” attribute to set height of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

Height can be either “MATCH_PARENT” or “WRAP_CONTENT” or any fixed value.
Learn to Set Height of NestedScrollView Dynamically

Set Padding of NestedScrollView

We use android:padding=”” attribute to set padding of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        />

Here, we have set padding of 10dp in NestedScrollView using android:padding=”” attribute.
Learn to Set Padding of NestedScrollView Dynamically

Set Margin of NestedScrollView

We use android:layout_margin=”” attribute to set margin of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        />

Here, we have set margin of 10dp in NestedScrollView using android:layout_margin=”” attribute.
Learn to Set Margin of NestedScrollView Dynamically

Set Background of NestedScrollView

We use android:background=”” attribute to set background of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        />

Here, we have set background of color #ff0000 in NestedScrollView using android:background=”” attribute.
Learn to Set Background of NestedScrollView Dynamically

Set Visibility of NestedScrollView

We use android:visibility=”” attribute to set visibility of NestedScrollView.
We can do it as below –

    <NestedScrollView
        android:id="@+id/nestedScrollView_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />

Here, we have set visibility of NestedScrollView using android:visiblity=”” attribute. Visibility can be of three types – gone, visible and invisible
Learn to Set Visibility of NestedScrollView Dynamically

Till now, we have see how to use android NestedScrollView using Kotlin. We have also gone through different attributes of NestedScrollView to perform certain task. Let’s have a look at list of such attributes and it’s related task.

Different Attributes of Android NestedScrollView Widget

Attributes of NestedScrollView as same as scrollView.

We have seen different attributes of NestedScrollView and how to use it. If you wish to visit post to learn more about it

Thus, we have seen what is NestedScrollView, how can we use android NestedScrollView using Kotlin ? etc. We also went through different attributes of android NestedScrollView.

Leave a Reply