How to check whether ScrollView is scrollable

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
        }
    }
});

scrollView.viewTreeObserver
        .addOnGlobalLayoutListener {

            val isScrollable = scrollView.canScrollVertically(1)
        }

Tags:

Android