check if an android scrollview can scroll
Thanks to: @johanvs and https://stackoverflow.com/a/18574328/3439686
private boolean canScroll(HorizontalScrollView horizontalScrollView) {
View child = (View) horizontalScrollView.getChildAt(0);
if (child != null) {
int childWidth = (child).getWidth();
return horizontalScrollView.getWidth() < childWidth + horizontalScrollView.getPaddingLeft() + horizontalScrollView.getPaddingRight();
}
return false;
}
private boolean canScroll(ScrollView scrollView) {
View child = (View) scrollView.getChildAt(0);
if (child != null) {
int childHeight = (child).getHeight();
return scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
}
return false;
}
I used the following code inspired by https://stackoverflow.com/a/18574328/3439686 and it works!
ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
int childHeight = ((LinearLayout)findViewById(R.id.scrollContent)).getHeight();
boolean isScrollable = scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
In addition to @johanvs response:
You should wait for view beign displayed
final ScrollView scrollView = (ScrollView) v.findViewById(R.id.scrollView);
ViewTreeObserver viewTreeObserver = scrollView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int childHeight = ((LinearLayout) v.findViewById(R.id.dataContent)).getHeight();
boolean isScrollable = scrollView.getHeight() < childHeight + scrollView.getPaddingTop() + scrollView.getPaddingBottom();
if (isScrollable) {
//Urrah! is scrollable
}
}
});