ScrollView has a method for setting the x and y scroll offset, but no method for getting the current offset (all I'm really interested is the y offset, since ScrollView only supports vertical scrolling). I don't see any method that would work in the superclasses, and tried getTop() for the content view, which is always zero. Am I missing something?
5 Answers
Call getScrollY()
on the ScrollView
See here for the documentation: http://developer.android.com/reference/android/view/View.html#getScrollY%28%29

- 42,564
- 15
- 187
- 127

- 7,184
- 3
- 32
- 38
-
2I wanted to know whether my scrollView wrapping a textView with periodically increasing content (like a log view) was showing the last line of text. This did it: `boolean lastLineVisible = scrollView.getScrollY() + scrollView.getHeight() + 100 >= textView.getHeight()` – Stan Kurdziel Jul 02 '15 at 01:17
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);
}
});

- 182
- 2
- 9
-
1অস্থির সল্যুশন, Amazing solution.... Thank's, I love your solution. After 6 years of Android Apps Development, finally I found that solution. – Ahamadullah Saikat Oct 26 '20 at 09:43
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.

- 167
- 1
- 9
What about: computeHorizontalScrollOffset()
and computeVerticalScrollOffset()
.

- 198,401
- 62
- 356
- 264
-
4Those methods are protected and thus only accessible if you're subclassing ScrollView. – Charles Harley May 11 '11 at 14:56
-
Thanks, Cristian, your solution helped to me. I have already had a subclassed view from ScrollView. It should be calculated with a listener setOnScrollViewListener (http://www.nicholasmelnick.com/entries/104). Also a first solution with getScrollY() works right. – CoolMind Nov 02 '15 at 15:31
I achieve to get by the following. First get screen heigh and width.
DisplayMetrics metrics = getResources().getDisplayMetrics();
float screenWidth = metrics.widthPixels;
float screenHeight = metrics.heightPixels;
then to find out the document height (the total, what is being shown and what is out of the screen)
public float getDocumentHeight(){
return (computeVerticalScrollRange() * screenHeight)/computeVerticalScrollExtent();
}
Finally to get the offset
public float getTopY(){
return (getDocumentHeight() * computeVerticalScrollOffset())/computeVerticalScrollRange();
}
This give you the top of the window relative to the part of the document you are seeing so you could find the exact position of an event by
public boolean onTouchEvent(MotionEvent event) {
int y = event.getY() + (int) getTopY();
}
You can also do something similar to handle the width

- 779
- 10
- 16