How to programmatically setLockDrawerMode

552 views Asked by At

I have created a toggle drawer where the drawer will slide from the right automatically when the user has clicked on the item displayed in the content view. However, at this point, the drawer can be manually toggled( user can slide open the drawer by swiping from the edge of the screen) open by user as well as toggled open automatically when user clicked on the displayed item. Hence, I have implemented the following code line as mDrawer.setDrawerLockMode(1, GravityCompat.END); whereby "1" is defined as:


public static final int LOCK_MODE_LOCKED_CLOSED

The drawer is locked closed. The user may not open it, though the app may open it programmatically.

Constant Value: 1 (0x00000001)


However, when debugged, the result is that user can still manually toggle open the drawer and the drawer can still be toggled automatically when item is clicked.

What is actually the desired toggle result is that only the drawer can be toggled out when the item has been selected and not to give user the privilege to manually toggle the drawer.

Hence, could anyone please help to see what wrong or offer some constructive suggestions?

I have attached the following code for your perusal:

mDrawerToggle = new CustomActionBarDrawerToggle(getActivity(), mDrawer);
mDrawer.openDrawer(GravityCompat.END);
mDrawer.setDrawerListener(mDrawerToggle);
//To lock the drawer from being manually toggled
mDrawer.setDrawerLockMode(1, GravityCompat.END);
1

There are 1 answers

9
JLONG On BEST ANSWER

You can try doing this:

First Call the layout of your drawer:

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

After that set the lock mode like so:

mDrawerLayout.setDrawerLockMode(mDrawerLayout.LOCK_MODE_LOCKED_CLOSED);
getActionBar().setHomeButtonEnabled(false); // This for the App Icon 

Then if you want to unlock your drawer again:

   mDrawerLayout.setDrawerLockMode(mDrawerLayout.LOCK_MODE_UNLOCKED); // It is unlocked but it is not shown.

or

mDrawerLayout.setDrawerLockMode(mDrawerLayout.LOCK_MODE_LOCKED_OPEN); // To Show the drawer opened but it will stay open.

See if this helps you. :)

EDIT 2:

Now I get it:

Declare your drawer layout and your drawer listview:

mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mDrawerList = (ListView)findViewById(R.id.list_slidermenu);

On your button/item listener do the following:

   yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mDrawerLayout.openDrawer(mDrawerList); // This will open the button on click of the item

        }
    });

For the rationality of the Unlock, I just showed it just incase you want to open the drawer back again.

Don't lock your button, just try my edit first. see if that helps you.

EDIT 3:

Set your Drawer listview and Drawer layout:

    mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
    mDrawerList = (ListView)findViewById(R.id.list_slidermenu);
    mDrawerLayout.setDrawerLockMode(mDrawerLayout.LOCK_MODE_LOCKED_CLOSED); //prevents user from manually opening the drawer
   getActionBar().setHomeButtonEnabled(false); // Prevents user from opening the drawer using the app icon

Then add this on your listener:

 yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mDrawerLayout.openDrawer(mDrawerList); // This will open the button on click of the item

        }
    });

I tested this on my app, I believe this the thing you wanted. So basically the drawer will not open even if the user will try to swipe or click the app icon but it will open once an item is clicked on the content view. which is I believe your desired output.