Open In App

How to Detect End of ScrollView in Android?

Last Updated : 15 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
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: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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads