1

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);
Community
  • 1
  • 1
androidnoob
  • 79
  • 1
  • 1
  • 9
  • When you say "manually toggled", do you mean the user can slide the drawer open by swiping from the edge of the screen? – Karakuri Nov 21 '14 at 04:06
  • yes, manually toggle in this instance is that the user can slide the drawer open by swiping from the edge of the screem – androidnoob Nov 21 '14 at 04:14

1 Answers1

0

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.

JLONG
  • 805
  • 10
  • 20
  • it is not working, I have implemented the following code {mDrawer.setDrawerLockMode(mDrawer.LOCK_MODE_LOCKED_CLOSED); getActivity().getActionBar().setHomeButtonEnabled(false);} and the following debugging result happens: user can only manually toggle the drawer and drawer doesn't toggle automatically when the item is clicked – androidnoob Nov 21 '14 at 07:02
  • and I would like to know what is the rationality for me to unlick the drawer? thanks – androidnoob Nov 21 '14 at 07:02
  • I will try out your solution and update you on the progress. Thanks – androidnoob Nov 22 '14 at 01:48
  • I have tried your solution, however, I am still facing the same issue, whereby the sliding drawer will still slide in automatically both manually and when user clicks on the item displayed in the content view – androidnoob Nov 24 '14 at 08:01
  • Enlighten me, you want to automatically open your drawer when the user clicks on the item on your content view and not allowing the user open the drawer when nothing is click is that correct? – JLONG Nov 24 '14 at 09:11
  • Yes, that idea is correct. Right drawer will automatically slide into view only when the user clicks on an item in the content view. At this point in time, the right drawer can slide out when item in the content view is clicked and when user manually pulls the drawer from the edge of the screen. – androidnoob Nov 24 '14 at 11:49
  • Oh, ok I got it, add this: mDrawerLayout.setDrawerLockMode(mDrawerLayout.LOCK_MODE_LOCKED_CLOSED); getActionBar().setHomeButtonEnabled(false); – JLONG Nov 25 '14 at 02:17
  • The following code above would prevent the user from manually sliding the drawer but it also prevent the slider from sliding out automaticaaly when the item is clicked.sorry to trouble you – androidnoob Nov 25 '14 at 03:24
  • Thanks!!it's working and I have realised where I have gone wrong in my initial coding!! Thanks for your trouble!! could you be so kind to upvote my question.thank you – androidnoob Nov 25 '14 at 07:52