Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java for Android
  • Android Studio
  • Android Kotlin
  • Kotlin
  • Flutter
  • Dart
  • Android Project
  • Android Interview
Open In App
Next Article:
How to Detect Swipe Direction in Android?
Next article icon

How to Detect End of ScrollView in Android?

Last Updated : 15 Feb, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

ScrollView in Android

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.


Next Article
How to Detect Swipe Direction in Android?
author
aashaypawar
Improve
Article Tags :
  • Kotlin
  • Android

Similar Reads

  • How to Detect Swipe Direction in Android?
    Detecting gestures is a very important feature that many app developers focus on. There can be a number of gestures that could be required to perform certain actions. For example, a user might need to swipe the screen from left to right to unlock the screen. Similarly, vice-versa might be needed. In
    3 min read
  • How to Detect Long Press in Android?
    A Long Press refers to pressing a physical button or tap a virtual button on a touchscreen and holding it down for a second or two. Employed on touchscreens, smartphones, tablets, and smartwatches, the long press or long tap increases the user interface's flexibility. The typical "short press" or "s
    4 min read
  • How to Disable GridView Scrolling in Android?
    A GridView is a ViewGroup that can display data from a list of objects or databases in a grid-like structure consisting of rows and columns. Grid view requires an adapter to fetch data from the resources. This view can be scrolled both horizontally and vertically. The scrolling ability of the GridVi
    4 min read
  • How to Make TextView Scrollable in Android?
    In Android, a TextView is a primary UI element used to display text present in the form of numbers, strings, and paragraphs. However, for a large amount of information to be displayed size adjustment doesn't work. So in this article, we will show you how to make TextView scrollable on Android. Follo
    8 min read
  • How to Detect User Inactivity in Android?
    It is important to detect user inactivity in applications that display or contain private credentials, such as social apps, banking apps, wallet apps, etc. In such applications, there is a login session that authenticates log-in credentials. Once the session starts, the user can perform desired acti
    3 min read
  • How to Get Touch Coordinates of Screen in Android?
    In Android, the app screen is a layout that can be used to display various UI elements like TextView, Button, ImageView, etc. These UI elements are positioned according to a set of pre-defined rules and developer changes. In simple words, each bit on any app screen defines a coordinate that can be u
    3 min read
  • ScrollView in Android
    In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views ins
    2 min read
  • Zoom Scroll View in Android
    In this article, we are going to implement Zoom on ScrollView. Most of the time when we create a scroll view then it contains a lot of data and if we want to view any content on zooming then we can implement this feature. This feature can be useful when we are scrolling in an App and that contains d
    3 min read
  • How to Detect Touch Event on Screen Programmatically in Android?
    Detecting a touch confirms that the screen is fully functional. Responding to touch is something that a developer deals with. As Android devices have a touch-based input, things are programmed upon application of touch. For explicitly calling methods within the application, a touch action must be re
    5 min read
  • How to Create Blink Effect on TextView in Android?
    In this article, we are going to implement a very important feature related to TextView. Here we are adding the blink text feature on a TextView. This feature can be used to show important announcements or notifications in an App. Even we can add this feature to show important links for the user. So
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences