Android DrawerLayout.setDrawerLockMode() not working
Asked Answered
C

3

12

I have a Navigation Drawer (appcompat v7) in my app which is working perfectly fine.

Now I want to disable it, until the user buys an in-app-purchase to unlock additional functionality. So in my Activity.onCreate(), after initializing the drawer and populating it, I am calling this function:

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

This function is not doing anything. The drawer continues to open and close as normal after tapping the drawer carat in the actionbar. I tried calling this function in Activity.onResume() without any difference.

What is the correct way to use this function? (I tried looking online for answers, but couldn't find anything which addresses my issue). Any help is appreciated, as I am stuck on this issue for quite sometime now.

Christiansand answered 22/11, 2014 at 4:40 Comment(0)
C
9

mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

is only disabling the opening drawer layout by swiping till you click navigation drawer icon keep a boolean variable

write mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); in onStart() and also write below lines of code

  @Override
    public boolean onOptionsItemSelected(android.view.MenuItem item) {

        if(!disabled)
        {
        if (item.getItemId() == android.R.id.home) {

            if (mDrawerLayout.isDrawerOpen(mDrawerLinearLayout)) {
                mDrawerLayout.closeDrawer(mDrawerLinearLayout);
            } else {
                mDrawerLayout.openDrawer(mDrawerLinearLayout);
            }
        }
        }
        return super.onOptionsItemSelected(item);
    }

this will work for sure

Connor answered 22/11, 2014 at 5:12 Comment(3)
onOptionsItemSelected is called when an item from the Overflow menu is selected, not when an item from the Navigation Drawer is selected.Christiansand
Thats why i wrote item.getItemId() == android.R.id.home it is aslo called when home button on navigation drawer clickedConnor
@SHASHIDHARMANCHUKONDA help me this #27896068Corenecoreopsis
J
3

When you call setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) it locks opening and closing drawer only by swipes.

The drawer continues to open and close as normal after tapping the drawer carat in the action bar because your drawer will still respond to calls to openDrawer(int), closeDrawer(int) although a drawer is locked. You need to add some logic in your action bar menu button listener and not to call openDrawer(int) when you don't want it to open.

Btw, it is okay to call setDrawerLockMode(int) in onСreate

Janeljanela answered 10/3, 2016 at 7:24 Comment(0)
L
0

There is a bug with DrawerLayout and used gravity. I have reported it here:

https://issuetracker.google.com/issues/136738274

Likeminded answered 4/7, 2019 at 14:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.