i have a scroll view on my layout and many buttons after scrolling and choosing a button when it returns back to the layout the scroll view starts at the top. i wanted the scroll view to starts from where the user stops. please help am a beginner in android so please explain briefly.
Asked
Active
Viewed 1,389 times
2 Answers
1
You'll have to save your scroll view's position before your Activity or Fragment gets Destroyed.
You can save value on onSaveInstanceState
//save value on onSaveInstanceState
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putIntArray("SCROLL_POSITION",
new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
}
and then restore it on onRestoreInstanceState
//Restore them on onRestoreInstanceState
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
final int[] position = savedInstanceState.getIntArray("SCROLL_POSITION");
if(position != null)
mScrollView.post(new Runnable() {
public void run() {
mScrollView.scrollTo(position[0], position[1]);
}
});
}
The above is just an example for more details see THIS BLOG and This post on SO.

Community
- 1
- 1

Anirudh Sharma
- 7,968
- 13
- 40
- 42