Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggling DrawerLockMode of Android Navigation Drawer on rotation change.

I am following this post http://derekrwoods.com/2013/09/creating-a-static-navigation-drawer-in-android/

I want drawer to be opened when in landscape mode. Here is my onCreate function.

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_order);

    drawerList = (ListView) findViewById(R.id.left_drawer);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    FrameLayout frameLayout = (FrameLayout) findViewById(R.id.content_frame);

    if (((ViewGroup.MarginLayoutParams) frameLayout.getLayoutParams()).leftMargin == (int) getResources()
            .getDimension(R.dimen.drawer_size)) {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
        drawerLayout.setScrimColor(Color.TRANSPARENT);
        isDrawerLocked = true;
    }

    // Set the adapter for the list view
    // drawerItems = getResources().getStringArray(R.array.drawerOptions);
    drawerItems = DummyContent.ITEMS
            .toArray(new DummyItem[DummyContent.ITEMS.size()]);
    drawerList.setAdapter(new ArrayAdapter<DummyItem>(this,
            android.R.layout.simple_list_item_1, drawerItems));
    drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
            R.drawable.ic_drawer, R.string.action_short,
            R.string.action_short) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            getActionBar().setTitle("test");
            // ((FragmentInterface) fragment).showMenuActions();
            invalidateOptionsMenu();
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle("Select Option");
            // ((FragmentInterface) fragment).hideMenuActions();
            invalidateOptionsMenu();
        }
    };

    if (!isDrawerLocked) {
        drawerLayout.setDrawerListener(drawerToggle);
    }

    // Set the drawer toggle as the DrawerListener
    DrawerItemClickListener drawerItemClickListener = new DrawerItemClickListener();
    drawerList.setOnItemClickListener(drawerItemClickListener);

    if (!isDrawerLocked) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

Now the problem is whenever my activity is starting in portrait mode, after rotation drawer is collapsible (can be closed using sliding right to left gesture) even after setting DrawerLayout.LOCK_MODE_LOCKED_OPEN. This problem does not occur when opened directly in Landscape mode.

like image 529
Apurv Gupta Avatar asked Dec 23 '13 12:12

Apurv Gupta


1 Answers

I had to move that code to my onResume():

@Override
public void onResume() {
    super.onResume();

    Display display = getWindowManager().getDefaultDisplay();

    if (display.getRotation() == Surface.ROTATION_90 || display.getRotation() == Surface.ROTATION_270) {
        // Landscape
        isDrawerLocked = true;
         mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN);
    } else {
        // Portrait
        isDrawerLocked = false;
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
}
like image 180
elmango Avatar answered Oct 22 '22 14:10

elmango