18

How can I check if a ScrollView is higher than the screen? When the content of a ScrollView fits on the screen, the ScrollView isn't scrollable, when it's contents exceed the screen height it's scrollable. How can I check the condition of a ScrollView in that regard?

fweigl
  • 21,278
  • 20
  • 114
  • 205

4 Answers4

27

This is the code from ScrollView, which is private, but can be adapted to be used outside of the class itself

/**
 * @return Returns true this ScrollView can be scrolled
 */
private boolean canScroll() {
    View child = getChildAt(0);
    if (child != null) {
        int childHeight = child.getHeight();
        return getHeight() < childHeight + mPaddingTop + mPaddingBottom;
    }
    return false;
}
FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
11

Too late, but I'm using the following code and it looks more safe for me:

if (view.canScrollVertically(1) || view.canScrollVertically(-1)) {
    // you code here
}
Alex P
  • 131
  • 1
  • 6
5

A ScrollView always has 1 child. All you need to do is get the height of the child

 int scrollViewHeight = scrollView.getChildAt(0).getHeight();

and Calculate Height of Your Screen

if both are equal(or scrollView Height is more) then it fits on your screen.

T_V
  • 17,440
  • 6
  • 36
  • 48
  • 4
    that is if your scrollview is covering the entire screen. – string.Empty Sep 02 '13 at 12:59
  • @NicolasTyler only entire height of screen. and If the scrollview is not on entire height of screen then you would know what is the size of your scroll view. Everything can be calculated,give some math. – T_V Sep 02 '13 at 13:07
  • Although late for this, why not something like this: `bool childLargerThanParent = scrollView.getChildAt(0).getHeight() > scrollView.getHeight();`? More safe and less _calculations_. – auhmaan May 18 '17 at 09:38
5

In my case, I was checking to see if my scrollView(which contained text) was scrollable vertically when the activity was created. On phones, it would scroll but on tablets it couldn't. canScrollVertically was returning me incorrect value because it couldn't be determined yet. I fixed this issue by calling it in the OnGlobalLayoutListener.

(Kotlin)

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    // Must use onGlobalLayout or else canScrollVertically will not return the correct value because the layout hasn't been made yet
    scrollView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {

        override fun onGlobalLayout() {
            // If the scrollView can scroll, disable the accept menu item button
            if ( scrollView.canScrollVertically(1) || scrollView.canScrollVertically(-1) )
                acceptMenuItem?.isEnabled = false

            // Remove itself after onGlobalLayout is first called or else it would be called about a million times per second
            scrollView.viewTreeObserver.removeOnGlobalLayoutListener(this)
        }
    })
}

My use case was displaying terms of use. I didn't want the accept button to be enabled until the user scrolled to the bottom. I know this is late but i hope this resolves some confusion about canScrollVertically

alexrnov
  • 2,346
  • 3
  • 18
  • 34
James
  • 4,573
  • 29
  • 32
  • This is the perfect solution. Awesome you save my day. – Joker Jul 16 '21 at 11:38
  • In my tests, `canScrollVertically(...)` returns `false` inside `onGlobalLayout()` even when the contents are scrollable. Which means that the button is enabled from the beginning, regardless of whether the user has scrolled to the bottom or not. – user2891462 Sep 02 '21 at 13:33