17

I was wondering, how to check whether the current ScrollView is scrollable? It seems that, there isn't public method called canScroll or isScrollable in ScrollView.

Scrollable : You can move the ViewGroup inside the ScrollView up and down, and the scroll bar will be visible when you move it up and down. So, if there is only little rows in the ScrollView, and they can fit inside single screen, ScrollView is not scrollable then.

2
  • Scrollable - You can move the ViewGroup inside the ScrollView up and down, and the scroll bar will be visible when you move it up and down. Commented Oct 21, 2013 at 20:09
  • that's what a scrollview is about, i don't get your question Commented Oct 21, 2013 at 20:26

3 Answers 3

22

You can do some little math to calculate the views raw height and the height of the content. If the difference of this heights is < 0 the view is scrollable.

To calculate the raw height you can use View.getMeasuredHeight(). Because ScrollView is a ViewGroup and has max one child, get the height of that child with ViewGroup.getChildAt(0).getHeight();

Use a ViewTreeObserver to get the heights, because it will be called at the moment the layout / view is changing the visibility, otherwise the heights could be 0.

ScrollView scrollView = (ScrollView)findViewById(R.id...); ViewTreeObserver observer = scrollView.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int viewHeight = scrollView.getMeasuredHeight(); int contentHeight = scrollView.getChildAt(0).getHeight(); if(viewHeight - contentHeight < 0) { // scrollable } } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Is there any reason using scrollView.getChildAt(0).getHeight(); and not scrollView.getChildAt(0).getMeasuredHeight();
@CheokYanCheng In this case there is no difference between this methods. As I tested it the values are equal. But if you change the layout params in Java code use getHeight() to be sure, the right values return.
6
scrollView.viewTreeObserver .addOnGlobalLayoutListener { val isScrollable = scrollView.canScrollVertically(1) } 

Comments

0

I think I might be missing something, but shouldn't it be as simple as checking if

scrollView.getHeight() >= parentView.getMeasuredHeight() 

you might actually need: scrollView.getChildAt(0).getHeight() and/or parentView.getHeight() instead, but the idea is the same.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.