38

Possible Duplicate:
How to trigger an event when scrollView reach the bottom with Android?

I have been trying for an hour or so to detect when my scrollView gets to the bottom of the screen. Due to the various screen sizes and what not involved with Android, I can't simply say it's at the bottom when its Y position reaches a certain value, but there's no method that detects when it is at the bottom that I found.

I'm sure there's a simple solution to this, subtracting the height of the view by some other variable or something, but for some reason it just isn't clicking for me. Any ideas would be appreciated, thank you.

Community
  • 1
  • 1
Peacecraft
  • 433
  • 1
  • 5
  • 6

1 Answers1

82

Try this:

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) 
{
    // Grab the last child placed in the ScrollView, we need it to determinate the bottom position.
    View view = (View) getChildAt(getChildCount()-1);

    // Calculate the scrolldiff
    int diff = (view.getBottom()-(getHeight()+getScrollY()));

    // if diff is zero, then the bottom has been reached
    if( diff == 0 )
    {
        // notify that we have reached the bottom
        Log.d(ScrollTest.LOG_TAG, "MyScrollView: Bottom has been reached" );
    }

    super.onScrollChanged(l, t, oldl, oldt);
}

To implement this, extend ScrollView and then override the onScrollChanged method (inherited from View).

Reference: Android: Understanding when ScrollView has reached the bottom

d.danailov
  • 8,760
  • 4
  • 47
  • 35
Harry Joy
  • 55,133
  • 29
  • 149
  • 204
  • 29
    In my experience, you should check for if(diff <= 0) – littleK Feb 21 '12 at 21:15
  • 1
    Makes sense if it happens to return -1. – BlackHatSamurai Aug 24 '12 at 00:55
  • 10
    just to give credit, answer was copied from http://www.marteinn.se/blog/?p=485 – Gabor May 20 '13 at 11:31
  • Reference above is now:http://www.marteinn.se/blog/android-determinate-when-scrollview-has-reached-the-bottom/ and was very helpful. – steven smith Dec 25 '14 at 19:32
  • 7
    A ScrollView can host only one direct child so the line and Cast: (View) getChildAt(getChildCount()-1); is unnecessary. – drindt Dec 27 '14 at 19:41
  • Nice! thanks! works excellent – superUser May 16 '15 at 18:50
  • There's no need to extend `ScrollView`. You can add a scroll listener with `myScrollView.getViewTreeObserver().addOnScrollChangedListener()` – yuval Jul 28 '15 at 19:02
  • 1
    You can also override onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) and check if clampedY is true – Julian Oct 06 '15 at 21:26
  • `if(diff <= 0)` triggers around 10 times when scrolled on my phone. @littleK can you elaborate on why we should check for <=? – Alexandre G Dec 08 '15 at 06:01
  • Needs to be specifically noted that bottom - (height + scrollposition) formula is correct only for the direct-child container of the scroll view. The formula breaks if attempted to calculate on actual UI components that are further down the hierarchy. – Nar Gar Aug 09 '16 at 04:54
  • Please refer this link;Hope it will helps,http://stackoverflow.com/questions/10316743/detect-end-of-scrollview – MohanRaj S Nov 03 '16 at 11:04