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?

- 21,278
- 20
- 114
- 205
-
a scrollview already cannot be scrolled when the contents fits in the screen – string.Empty Sep 02 '13 at 12:02
-
this is default behavior – Shivang Trivedi Sep 02 '13 at 12:02
-
2I know this is the normal and logical behaviour, I want to know if theres a method to check wether the ScrollView fits the screen or exceeds it. – fweigl Sep 02 '13 at 12:06
4 Answers
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;
}

- 10,908
- 1
- 31
- 37
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
}

- 131
- 1
- 6
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.

- 17,440
- 6
- 36
- 48
-
4
-
@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
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
-
-
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