How to Detect End of ScrollView in Android?
Last Updated :
15 Feb, 2022
In Android, a ScrollView is a view that lets the user scroll up and down to visit elements declared inside it. ScrollView is most commonly used to display TextView that contains a large amount of text which does not fit on a single instance of a screen. Users can scroll down and up to read complete text from the TextView. ScrollViews are also used in forms where the application requires users to read each and every term and condition before agreeing to it. Unless the bottom is reached, the user cannot proceed as buttons are disabled. An example is shown below.
So in this article, we will show you how you could create a function to detect if the user has reached the end of the ScrollView in Android. Follow the below steps once the IDE is ready.
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. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add a String
Navigate to app > res > values > strings.xml and add a sample string as shown below.
XML
<resources>
<string name="app_name">GFG | ScrollViewEnd</string>
<string name="my_data">"Text Goes Here"</string>
</resources>
Step 3: Working with the activity_main.xml file
Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file. Add a TextView inside a ScrollView as shown below. Set the text of the TextView to the string that we created in the above code.
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/scroll_view_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
<TextView
android:id="@+id/text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/my_data"/>
</ScrollView>
</RelativeLayout>
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package org.geeksforgeeks.scrollviewend
import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import android.widget.ScrollView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
// Extend Touch Listener and Scroll Listener
class MainActivity : AppCompatActivity(), View.OnTouchListener, ViewTreeObserver.OnScrollChangedListener {
// Declaring ScrollView from the layout file
private lateinit var mScrollView: ScrollView
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Initializing the ScrollView
mScrollView = findViewById(R.id.scroll_view_1)
// Invoking touch listener to detect movement of ScrollView
mScrollView.setOnTouchListener(this)
mScrollView.viewTreeObserver.addOnScrollChangedListener(this)
}
// We want to detect scroll and not touch,
// so returning false in this member function
@SuppressLint("ClickableViewAccessibility")
override fun onTouch(p0: View?, p1: MotionEvent?): Boolean {
return false
}
// Member function to detect Scroll,
// when detected 0, it means bottom is reached
override fun onScrollChanged() {
val view = mScrollView.getChildAt(mScrollView.childCount - 1)
val topDetector = mScrollView.scrollY
val bottomDetector: Int = view.bottom - (mScrollView.height + mScrollView.scrollY)
if (bottomDetector == 0) {
Toast.makeText(baseContext, "Scroll View bottom reached", Toast.LENGTH_SHORT).show()
}
if (topDetector <= 0) {
Toast.makeText(baseContext, "Scroll View top reached", Toast.LENGTH_SHORT).show()
}
}
}
Output:
You can see that when we scroll down to reach the bottom, a Toast message appears indicating the bottom is reached. The same is followed when the top is reached.
Similar Reads
Architecture of 8085 microprocessor A microprocessor is fabricated on a single integrated circuit (IC) or chip that is used as a central processing unit (CPU).The 8085 microprocessor is an 8-bit microprocessor that was developed by Intel in the mid-1970s. It was widely used in the early days of personal computing and was a popular cho
11 min read
Android Architecture Android architecture contains a different number of components to support any Android device's needs. Android software contains an open-source Linux Kernel having a collection of a number of C/C++ libraries which are exposed through application framework services. Among all the components Linux Kern
5 min read
States of a Process in Operating Systems In an operating system, a process is a program that is being executed. During its execution, a process goes through different states. Understanding these states helps us see how the operating system manages processes, ensuring that the computer runs efficiently. Please refer Process in Operating Sys
11 min read
Android Tutorial In this Android Tutorial, we cover both basic and advanced concepts. So whether you are a fresher (graduate) or an experienced candidate with several years of Android Development experience, you can follow this Android tutorial to kick-start your journey in Android app development. Our Android Tutor
15+ min read
Activity Lifecycle in Android with Demo App In Android, an activity is referred to as one screen in an application. It is very similar to a single window of any desktop application. An Android app consists of one or more screens or activities. Each activity goes through various stages or a lifecycle and is managed by activity stacks. So when
9 min read
Introduction to Android Development Android operating system is the largest installed base among various mobile platforms across the globe. Hundreds of millions of mobile devices are powered by Android in more than 190 countries of the world. It conquered around 71% of the global market share by the end of 2021, and this trend is grow
5 min read
Android UI Layouts Layouts in Android define the user interface and hold UI controls or widgets that appear on the screen of an application. Every Android application consists of View and ViewGroup elements. Since an application contains multiple activitiesâeach representing a separate screenâevery activity has multip
5 min read
Top 50 Android Interview Questions and Answers - SDE I to SDE III A Linux-based open-source OS, Android was created by Andy Rubin and became one of the most popular smartphone operating systems. With 71 percent of the market share worldwide, Android is on top. Because it is on top in the smartphone OS, Android development is always in demand.If you are seeking a j
15+ min read
Components of an Android Application There are some necessary building blocks that an Android application consists of. These loosely coupled components are bound by the application manifest file which contains the description of each component and how they interact. The manifest file also contains the appâs metadata, its hardware confi
3 min read
Kotlin Tutorial This Kotlin tutorial is designed for beginners as well as professional, which covers basic and advanced concepts of Kotlin programming language. In this Kotlin tutorial, you'll learn various important Kotlin topics, including data types, control flow, functions, object-oriented programming, collecti
4 min read