简体   繁体   中英

How to open Navigation Drawer when click on navigation drawer icon

How to open Navigation Drawer when click on navigation drawer icon.

I want open Navigation Drawer like this

![Navigation Drawer][1]

I added a menu but still not showing

toggle = new ActionBarDrawerToggle(
            DashBordActivity.this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    if (navigationView != null) {
        setupDrawerContent(navigationView);
    }


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
//@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

public void setupToolbar(String title) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(title);
    toolbar.setTitleTextColor(Color.BLACK);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

private void setupDrawerContent(NavigationView navigationView) {
    //revision: this don't works, use setOnChildClickListener() and setOnGroupClickListener() above instead
    navigationView.setNavigationItemSelectedListener(
            new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    menuItem.setChecked(true);
                    drawer.closeDrawers();
                    return true;
                }
            });
}

navigation drawer is not open when i click on the drawer how to solve this problem

is there any code is missing in my code.

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
       toolbar.setNavigationIcon(R.drawable.menu);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawer.openDrawer(START);
            }
        });

Try this,

<item
        android:id="@+id/nav_camera"
        android:icon="@drawable/ic_menu_camera"
        android:title="@string/import" />

</group>

Try this:

This tutorial is very help full like:-

  • Add the navigation drawer layout
  • set icon and text in drawer layout

I refer this link

https://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

it is very helpful try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM