Clicking Hamburger Icon on Toolbar Does Not Open Navigation Drawer

Clicking Hamburger Icon does not open Navigation Drawer

You need to implement onOptionsItemSelected in the VisitingPlace activity as well.
Are you implementing this?

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}

Clicking hamburger icon on Toolbar does not open Navigation Drawer

In your ActivityMain.xml, the toolbar is outside of the DrawerLayout. That's the problem. If you want Toolbar to interact with DrawLayout, Toolbar needs to be a child of DrawerLayout.

To fix the problem, make DrawerLayout the root of your activity. Here's the documentation. The relevant quote is:

To add a navigation drawer, declare your user interface with a
DrawerLayout object as the root view of your layout. Inside the
DrawerLayout, add one view that contains the main content for the
screen (your primary layout when the drawer is hidden) and another
view that contains the contents of the navigation drawer.

So basically, structure your ActivityMain.xml to be something like this:

<android.support.v4.widget.DrawerLayout ...>

<RelativeLayout ...>

<android.support.v7.widget.Toolbar .../>

<!-- Your other content goes here -->

</RelativeLayout>

<android.support.design.widget.NavigationView .../>

</android.support.v4.widget.DrawerLayout>

That should take care of the problem.

Clicking hamburger icon on Toolbar does not open Navigation Drawer

You're using the four-parameter constructor for ActionBarDrawerToggle, which means you'll have to call the toggle's onOptionsItemSelected() method in MainActivity's onOptionsItemSelected() override in order to open/close the drawer.

For example:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}

return super.onOptionsItemSelected(item);
}

If you happen to be providing your own Toolbar – e.g., as the support ActionBar (though it's not necessary to set it as such) – then you can instead pass that Toolbar as the third argument in the ActionBarDrawerToggle constructor call. For example:

Toolbar toolbar = findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

The drawer opening/closing will then be handled by ActionBarDrawerToggle internally, and you won't need to call through to the toggle in onOptionsItemSelected().

The setDisplayHomeAsUpEnabled() call is also unnecessary for this setup, which is handy if you don't want to set the Toolbar as the ActionBar.

on clicking hamburger icon navigation drawer not opening

Try this

ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open
, R.string.close) {
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}

@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
};


Related Topics



Leave a reply



Submit