Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

How to save the position of a ScrollView when the orientation changes in Android July 28, 2011

Filed under: Android,Coding — Rupert Bates @ 2:29 pm

To save and restore the scroll position of a ScrollView when the phone orientation changes you can do the following:
Save the current position in the onSaveInstanceState method:

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putIntArray("ARTICLE_SCROLL_POSITION",
                new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
    }

Then restore the position in the onRestoreInstanceState method, note that we need to post a runnable to the ScrollView to get this to work:

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        final int[] position = savedInstanceState.getIntArray("ARTICLE_SCROLL_POSITION");
        if(position != null)
            mScrollView.post(new Runnable() {
                public void run() {
                    mScrollView.scrollTo(position[0], position[1]);
                }
            });
    }
Advertisements
 

5 Responses to “How to save the position of a ScrollView when the orientation changes in Android”

  1. r Says:

    It made my work simple. I just copy-pasted, work complete.
    Thanks for the simple and useful post.

  2. Bosancero Says:

    if I copy & paste it, I become a warning: mScrollView cannot be resolved

    Plz help me, I’m just a beginner.
    My code:

    public class HadisiActivity extends Activity {

    protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putIntArray(“ARTICLE_SCROLL_POSITION”,
    new int[]{ mScrollView.getScrollX(), mScrollView.getScrollY()});
    }

    protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    final int[] position = savedInstanceState.getIntArray(“ARTICLE_SCROLL_POSITION”);
    if(position != null)
    mScrollView.post(new Runnable() {
    public void run() {
    mScrollView.scrollTo(position[0], position[1]);
    }
    });
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.hadisi_layout);
    }

  3. a4macro@yahoo.com Says:

    great… 🙂

  4. Thanks, it worked great

  5. Riley Says:

    How did you know that scrollTo had to be called from within a Runnable?


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s