Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if ScrollView is higher than screen / scrollable

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?

like image 320
fweigl Avatar asked Sep 02 '13 12:09

fweigl


3 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;
}
like image 68
FunkTheMonk Avatar answered Oct 30 '22 12:10

FunkTheMonk


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
}
like image 10
Alex P Avatar answered Oct 30 '22 12:10

Alex P


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.

like image 5
T_V Avatar answered Oct 30 '22 11:10

T_V