Question

I have two navigation drawers in my app enabled by two icons on the left and right side respectively. Now my right side icon is a menu item so I am able to toggle both the navigation drawers via onOptionsItemSelected.

The problem I am having is that when I open the right drawer and then I open the left drawer, they overlap. I would like to know how to close the right drawer when I click the nav drawer button on the left hand side (The probable home button next to the application icon). Is there a click listener for that button like the menu Item?

Thanks In Advance.

Was it helpful?

Solution

I found the solution. Initially, the ActionBarDrawerToggle Button (Home Button) is also a menu item. Thus, when we call:

mDrawerLayoout.onOptionsItemSelected() we can put the code inside there.

Here is full the code for that:

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    if (myDrawerToggle.onOptionsItemSelected(item)) {
        if (myDrawerLayout.isDrawerVisible(navDrawerRight) != false) {
        myDrawerLayout.closeDrawer(navDrawerRight);
    }
        return true;
    }

Notice I've put myDrawerLayout.isDrawerVisible(View drawer). That is because isDrawerOpened only works when the drawer is fully opened or closed. It's better to use isDrawerVisible() as it works when the drawer is in any state, opened, closed or in between.

Hope this helps someone out there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top