Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ScrollView .scrollTo not working? Saving ScrollView position on rotation

Tags:

android

Ok.. I must be overlooking something real simple here, but i think i'm trying to do something fairly basic.. Simply retain the scrollbar position of a ScrollView on orientation change...

Here is the code for my onSaveInstanceState and onRestoreInstanceState.. sView is the container for the ScrollView layout. Within my scrollview is a linearlayout with a lot of textviews.

    @Override 
public void onSaveInstanceState(Bundle outState) 
{
    //---save whatever you need to persist—

    outState.putInt("sViewX",sView.getScrollX());
    outState.putInt("sViewY",sView.getScrollY());

super.onSaveInstanceState(outState);

}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) 
{
    super.onRestoreInstanceState(savedInstanceState);

    sViewX = savedInstanceState.getInt("sViewX");   
    sViewY = savedInstanceState.getInt("sViewY");

    sView.scrollTo(sViewX, sViewY);

}

If I set a Toast with the values of sViewX and sViewY on the Restore, the values are kept and correct.

Edit: I just tried to do a sView.scrollTo(0,150); in my onCreate.. just to see if that would open the activity at 150px down, and it didn't. I think my issue has to do with the .scrollTo method.

like image 515
kefs Avatar asked Jul 16 '10 08:07

kefs


4 Answers

I figured it out.

Since I'm using setText to TextViews in my onCreate, calling .scrollTo won't work.

So now I'm using the following:

sView.post(new Runnable() {
    @Override
    public void run() {
        sView.scrollTo(sViewX, sViewY);
    } 
});
like image 147
kefs Avatar answered Nov 07 '22 01:11

kefs


onRestoreInstanceState() is just to early to scroll the view. That's why posting new Runnable helps, but not always. Sometimes one even have to use postDelayed() to let it work. For Fragment one can use onViewCreated() instead :

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    sViewX = savedInstanceState.getInt("sViewX");   
    sViewY = savedInstanceState.getInt("sViewY");
    sView.scrollTo(sViewX, sViewY);
}
like image 20
akd005 Avatar answered Nov 07 '22 01:11

akd005


This is working for me

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putInt(SystemGlobal.SCROLL_Y, mRelativeLayoutMain.getTop());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onRestoreInstanceState(savedInstanceState);
    mRelativeLayoutMain.scrollTo(0, savedInstanceState.getInt(SystemGlobal.SCROLL_Y));
}
like image 1
Anisetti Nagendra Avatar answered Nov 07 '22 00:11

Anisetti Nagendra


You should start scroll before components are drawn, since scroll does not work untill components are not created:

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    scrollView.scrollTo(.... 
like image 1
ivan.panasiuk Avatar answered Nov 07 '22 01:11

ivan.panasiuk