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.

Leave a Reply