Android: How to detect when a scroll has ended

Here is how I solved the problem. Hope this helps.

// declare class member variables
private GestureDetector mGestureDetector;
private OnTouchListener mGestureListener;
private boolean mIsScrolling = false;

public void initGestureDetection() {
        // Gesture detection
    mGestureDetector = new GestureDetector(new SimpleOnGestureListener() {
        public boolean onDoubleTap(MotionEvent e) {
            return true;

        public boolean onSingleTapConfirmed(MotionEvent e) {
            return true;

        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            // i'm only scrolling along the X axis
            mIsScrolling = true;                
            handleScroll(Math.round((e2.getX() - e1.getX())));
            return true;

         * Don't know why but we need to intercept this guy and return true so that the other gestures are handled.
        public boolean onDown(MotionEvent e) {
            Log.d("GestureDetector --> onDown");
            return true;

    mGestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {

            if (mGestureDetector.onTouchEvent(event)) {
                return true;

            if(event.getAction() == MotionEvent.ACTION_UP) {
                if(mIsScrolling ) {
                    Log.d("OnTouchListener --> onTouch ACTION_UP");
                    mIsScrolling  = false;

            return false;

    // attach the OnTouchListener to the image view

You should take a look at
Especially this could be of help (sorted by relevance):

getFinalY(); getFinalX(); and getCurrY() getCurrX()

This implies that you have to create a Scroller.

If you want to use touching you could also use GestureDetector and define your own canvas scrolling. The following sample is creating a ScrollableImageView and in order to use it you have to define the measurements of your image. You can define your own scrolling range and after finishing your scrolling the image gets redrawn.

Depending on your code you should consider invalidate(int l, int t, int r, int b); for the invalidation.


It seems to take place when a scroll ends (i.e. the user lets the finger go), that’s what I am using and it works great for me.

Leave a Comment