Android: Understanding when ScrollView has reached the bottom

(May 12th, 2010)

This snippet is pretty mutch as the title says, an example in detecting when ScrollView has reached the bottom. In this sample I extend ScrollView and then overrides the onScrollChanged method (inherited from View).

@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);
}

Cheers to this thread for helping me getting the calculations right.

Update:
You can find a full implementation here: InteractiveScrollView.java

11 Responses to “Android: Understanding when ScrollView has reached the bottom”

  1. Danilo Graciano says:

    Thank you! This code works fine.

  2. Thank you so much !! exactly what i needed !!

  3. Hi thats very nice tutorial on scrollview concept in android you can also check the one at
    android scrollview

    android styled scrollview

  4. Màrius Garcia says:

    it rocks !!!

  5. Tony says:

    Thank you, it works!
    But don’t forget about paddings.

  6. Ricardo says:

    Hi! Do you have a full implementation of this code? Thanks in advance!

  7. admin says:

    Hi Ricardo!
    I have updated the post with a link to a class I use.
    / M

  8. Emanuel says:

    Thanks!

    The idea works for horizontal scroll view as well.

  9. hamed says:

    in casting my scrollview to InteractiveScrollView it makes fatal excepion
    is there any problem in my casting

    InteractiveScrollView SV=(InteractiveScrollView)findViewById(R.id.scrollView1);

  10. hamed says:

    I got the problem
    my mistake was using scrollview in xml file and i thought that it can be cast!!!!!

    thanks

  11. admin says:

    Hi hamed! I’m glad you got it working. I have updated the class a little bit with a layout sample, and some refactoring, just to make it extra easy to use.

Leave a Reply