How to Detect Scroll Up and Down Gestures in a ListView in Android?
Last Updated :
26 Feb, 2023
A ListView in Android is a scrollable list used to display items. Data, which is a list or array of items fetched from a source or hardcoded in the application can be displayed row-wise using a ListView. If the items are less, then the list may not even reach the bottom of the screen. However, if the items are large in number, then they may go beyond the bottom of the screen, where a scroll function takes entry and lets users scroll up and down to discover the covered items.

In this article, we will show you how you could detect scrolls up and down in a ListView. 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: 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. We implemented a TextView that shall display the scrolling status and a ListView that shall display items.
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">
<TextView
android:id="@+id/text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Scroll Status"
android:textSize="20sp"
android:gravity="center"/>
<ListView
android:id="@+id/list_view"
android:layout_below="@id/text_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Step 3: Working with the MainActivity.kt file
In the main code, we declared an array list of elements (numbers from 1 to 30). Using an adapter this array is displayed into the ListView. A scroll listener is called on the ListView to get information on the first visible item, total visible items, and total item count. A buffer index variable is initially set to 0. Now, according to our algorithm, if the first visible element index is greater than the buffer variable, then we are basically scrolling down. Else we are scrolling up. Now refer to the below code.
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mList = arrayOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",)
val mListView = findViewById<ListView>(R.id.list_view)
val mTextView = findViewById<TextView>(R.id.text_view)
val mAdapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, mList)
mListView.adapter = mAdapter
mListView.setOnScrollListener(object : AbsListView.OnScrollListener{
override fun onScrollStateChanged(view: AbsListView?, scrollState: Int) {
// Do nothing
}
private var lastFirstVisibleItem = 0
override fun onScroll(view: AbsListView?, firstVisibleItem: Int, visibleItemCount: Int, totalItemCount: Int) {
if (lastFirstVisibleItem < firstVisibleItem) {
// Down
mTextView.text = "Scrolling down"
}
if (lastFirstVisibleItem > firstVisibleItem) {
// Up
mTextView.text = "Scrolling Up"
}
lastFirstVisibleItem = firstVisibleItem
}
})
}
}
Java
import android.app.Activity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
// MainActivity extends the built-in Activity class
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// Call the parent's onCreate method
super.onCreate(savedInstanceState);
// Set the content view to the activity_main layout
setContentView(R.layout.activity_main);
// Create a list of strings
String[] mList = new String[] {
"1", "2", "3", "4", "5", "6", "7", "8",
"9", "10", "11", "12", "13", "14", "15", "16",
"17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30"
};
// Get the ListView and TextView from the layout
ListView mListView = findViewById(R.id.list_view);
TextView mTextView = findViewById(R.id.text_view);
// Create an ArrayAdapter with the list of strings
ArrayAdapter<String> mAdapter = new ArrayAdapter<>(
this,
R.layout.support_simple_spinner_dropdown_item,
mList
);
// Set the adapter for the ListView
mListView.setAdapter(mAdapter);
// Set an OnScrollListener for the ListView
mListView.setOnScrollListener(
new AbsListView.OnScrollListener() {
// Store the first visible item from the previous scroll event
private int lastFirstVisibleItem = 0;
// Called when the scroll state changes
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// Do nothing
}
// Called when the ListView is scrolled
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// Check if the ListView is scrolling down
if (lastFirstVisibleItem < firstVisibleItem) {
mTextView.setText("Scrolling down");
}
// Check if the ListView is scrolling up
if (lastFirstVisibleItem > firstVisibleItem) {
mTextView.setText("Scrolling up");
}
// Update the first visible item
lastFirstVisibleItem = firstVisibleItem;
}
}
);
}
}
Output:
We can see that when we scroll down, the TextView displays "Scrolling Down". When we scroll up, it displays "Scrolling Up".
Similar Reads
How to Search an Item in ListView using EditText and TextWatcher in Android? Some applications provide a search bar for looking up any particular item from a list of items. Technically, a search bar is an EditText and an item list can be a ListView, RecyclerView, or a GridView that contains some items. Now, when a user types something in the EditText, the list must update in
3 min read
How to Dynamically Add Elements to a ListView in Android? ListView is a UI widget in android which is used in most android applications. We can display the list of data using the list view. We can dynamically add or remove items from the list view by performing some operations of the list. In this article, we will take a look at How to add Elements to a Li
4 min read
How to Add Footer to ListView in Android with Kotlin? In Android, a ListView is a view used to display a list of items separated by a stroke. A ListView adapter is used to supply items from the main code to the ListView in real-time. A Footer is anything that gets added at the bottom of any element. So, a Footer in ListView is a layout that is present
3 min read
How to Add Drag And Drop Feature in Android RecyclerView? Drag And Drop feature is very helpful. It is very simple and easy to implement. It is used in many apps that are famous, and if we're doing some project it is very convenient to implement it. You should know the RecyclerAdapter Class. A sample video is given below to get an idea about what we are go
4 min read
How to Detect Long Press on ListView Items in Android? ListView in Android is a ViewGroup that is used to display items in rows and has an adapter that inserts the desired elements into the list. Once the items are inserted into the ListView, they can be clicked and the desired action can be performed. As multiple operations can be performed on a ListVi
3 min read
How to Remove ListView Item Divider in Android? ListView in Android is generally used for displaying or listing items in the form of a list. Each item in the list is clickable and the list itself is vertically scrollable. In general, each item is separated by a thin line or a horizontal stroke. This line is termed an item divider in Android as it
3 min read
How to Add Fade and Shrink Animation in RecyclerView in Android? In this article, we are going to show the fade and shrink animation in RecyclerView. When we move downward then the item at the top will be fading out and then it will shrink. In the output, we can see how it is happening. We will be implementing Java/Kotlin programming language.Step by Step Impleme
15+ min read
How to Detect End of ScrollView in Android? 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
3 min read
How to Use Predictive Back Gesture in Android 13? A predictive back gesture is now available for Android devices with phones, big screens, and foldable in Android 13 (API level 33). This feature, which is a part of a multi-year release, will allow users to preview the destination or another outcome of a back gesture before fully completing it, givi
5 min read
How to Change Background Color of ListView Items in Android? In Android, a ListView is a layout element that is used to display items in a list. This is the simplest form of displaying a list or array of items and one can choose from pre-developed layouts for displaying an element without creating a separate layout unlike in other similar views. Each item in
2 min read