Prevent GridView from scrolling on onConfigurationChanged

482 views Asked by At

When configuration of the activity changes I apply this code -

public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        gridView.setNumColumns(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE ? 3:2);
        gridView.computeScroll();
    }

I want that if the GridView is in the midway, it will start from there only but as the configuration changes it is starting all over again from the top.

2

There are 2 answers

0
Biraj Zalavadia On

Add one line to your manifest file

No need to override onConfigChanges() method

<activity
            android:name="yourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
 />
0
Usman Kurd On

Take a flag and set it to false override method name onConfigurationChangedand in this method set flag true and check if flag is true then disable the scroll and then again false it you need to set it in menifest also code snipet is following

private boolean flag=false;



      @Override
        public void onConfigurationChanged(Configuration newConfig) {
            // TODO Auto-generated method stub
            Log.d("ONCONFIGCHANGE", "CALLED" );
            flag = true;
disableScroll();
            super.onConfigurationChanged(newConfig);
        }

for menifest do the following

android:configChanges="orientation"

and in onconfigchange use the following method

private void disableScroll()
{
gridview.setOnTouchListener(new OnTouchListener(){

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_MOVE){
            return true;
        }
        return false;
    }

});
}