How to get the current Y offset of a ScrollView
Solution 1:
Call getScrollY()
on the ScrollView
See here for the documentation: http://developer.android.com/reference/android/view/View.html#getScrollY%28%29
Solution 2:
Why don't you try something like this ?
targetScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
int scrollX = targetScrollView.getScrollX();
Log.d(TAG, "scrollX: " + scrollX);
}
});
Solution 3:
If you are certain that you should get some value after using getScrollY() or getTop(), try to put those method inside a
yourScroolView.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),"Current Y is : "+getScrollY,Toast.LENGTH_SHORT).show();
}
});
Now it should work. According to my understanding about this method, it will only run after the layout being drawn. That can be one of the reason why you kept getting 0 previously. Hope it helps.