MatusMak
MatusMak

Reputation: 560

Check if ScrollView is on the bottom, top or sides

How do I quickly and easily check if ScrollView or HorizontalScrollView is on its bottom, top or one of its side edges?

Upvotes: 1

Views: 2098

Answers (1)

MatusMak
MatusMak

Reputation: 560

As of API level 14 (Android 4.0 Ice Cream Sandwich), you can use View's methods canScrollHorizontally (int direction) and canScrollVertically (int direction).

Example usage:

//Check if ScrollView is at the bottom
//If not, scroll to the top
if(scrollView.canScrollVertically(1))
    scrollView.post(new Runnable() {

            @Override
            public void run() {
                scrollView.fullScroll(View.FOCUS_UP);
            }

        });

You can use this on ListView as well. For example, when working with messaging app, you may want to check if user is reading older messages, so when he will receive a new one, he won't be disrupted by scrolling to the bottom, but rather keeping him at current position.

Upvotes: 3

Related Questions