I have been trying to set the initial position of a ScrollView
but have not found a way. Does anyone have any ideas? Also, I have a GoogleMaps fragment as children of the ScrollView
.
-
Have you tried scrollview.setScrollY(
) during onResume? – Cory Roy Mar 10 '14 at 17:36 -
hey buddy thanks for the quick response but I just tried it on resume : scrollView = (ScrollViewExtend)findViewById(R.id.merchantScroll); scrollView.setScrollY(400); ... no luck : ( the only way that has worked for me was "onWindowFocusChanged" but this is only when the view is shown to the user so you can see the scroll to that certain position. – RyanCW Mar 10 '14 at 17:42
6 Answers
Yes, that is possible:
ScrollView.scrollTo(int x, int y);
ScrollView.smoothScrollTo(int x, int y);
ScrollView.smoothScrollBy(int x, int y);
can be used for that. The x and y parameters are the coordinates to scroll to on the horizontal and vertical axis.
Example in code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
sv.scrollTo(0, 100);
}
In that example, once the Activity
is started, your ScrollView
will be scrolled down 100 pixels.
You can also try to delay the scrolling process:
final ScrollView sv = (ScrollView) findViewById(R.id.scrollView);
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
sv.scrollTo(0, 100);
}
}, 250); // 250 ms delay

- 50,880
- 24
- 180
- 187
-
hey Philip I just tried it and it does not work for me : ScrollView scrollView32 = (ScrollView)findViewById(R.id.merchantScroll); scrollView32.scrollTo(0,300); – RyanCW Mar 10 '14 at 17:50
-
1Can you think of another reason why it may not be working? I have a button on screen that when clicked it will move the scrollView to the correct position(works) but I cannot set the initial position(doesn't work) – RyanCW Mar 10 '14 at 17:51
-
Are you adding other views to your scrollview at runtime? You could also try to delay the scolling, I will show you in an example above. – Philipp Jahoda Mar 10 '14 at 18:18
-
hey philip i actually implemented that before looking at your answer and it works : D but that is the accepted asnwer. – RyanCW Mar 10 '14 at 18:47
-
Maybe it was because you move before add the content or information to listview – DarckBlezzer May 19 '16 at 18:02
The accepted answer is not working for me. There is no direct way to set the initial position of a scroll view.
However, you can set the initial position before drawing the scroll view, like this:
rootView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
scrollView.getViewTreeObserver().removeOnPreDrawListener(this);
scrollView.setScrollY(100);
return false;
}
});
You can also use scrollView.setScrollY(100)
inside Handler
, but that will be jerky while scrolling.
-
This answer works almost fine in most situation, except launching app when screen is locked. Using ```onWindowFocusChanged``` is more stable. See https://stackoverflow.com/questions/10507976/android-setting-position-of-hortizontal-scrollview – Xavier.S May 31 '17 at 06:21
Scrollview sw = (ScrollView) findViewById(R.id.scrollView);
sw.post(new Runnable() {
public void run() {
sw.smoothScrollTo(0, 5000);
}
});
-
I your really think that this is THE solution, be proud and make a good edit with at least explanations. And please read stackoverflow documentation because this is not an answer. – N0un Sep 26 '17 at 11:41
None of the answers worked for me. My ScrollView
is part of a surrounding LinearLayout
. I put the adjustment of the ScrollView
into the onDraw()
method of this LinearLayout
like this:
@Override
protected void onDraw(Canvas canvas){
if(scrollAdjustTries > 2){
return;
}
scrollAdjustTries++;
mScrollView.scrollTo(0, mModel.scrollLine());
}
Very ugly, but working.

- 81
- 6
After several hours internet searching
this worked for me :
ScrollView.post(new Runnable() {
@Override
public void run() {
//setting position here :
ScrollView.scrollTo(X, Y);
}
});

- 312
- 3
- 10
The problem is that the ScrollView doesn't know its size before it been laid out. A solution is to save the scroll position unil onLayout has been called and then set the scroll position.
Here is an example (kotlin):
class MyScrollView constructor(context: Context, attrs: AttributeSet) : ScrollView(context, attrs) {
private var hasStartPositionBeenSet = false
var startPosition = 0
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
super.onLayout(changed, l, t, r, b)
if(!hasStartPositionBeenSet) {
scrollTo(0, startPosition)
hasStartPositionBeenSet = true
}
}
}
class MyActivity: Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
/* ... */
findViewById<MyScrollView>(R.id.scrollView).startPosition = scrollPosition
}
}

- 31
- 1