Navigation drawer item click listener not working

Well you can try this, you will need to do some stuff manually but that's the price of doing what you want. This is what you need to do:

  1. Delete the implementation of the NavigationView.OnNavigationItemSelectedListener, it will be not necesary
  2. After calling:

NavigationUI.setupWithNavController(navigationView, navController);

Insert this snippet:

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                int id=menuItem.getItemId();
                //it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
                if (id==R.id.nav_home){
                    Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();
                }
                //This is for maintaining the behavior of the Navigation view
                NavigationUI.onNavDestinationSelected(menuItem,navController);
                //This is for closing the drawer after acting on it
                drawer.closeDrawer(GravityCompat.START);
                return true;
            }
        });

I've made my own for the home fragment but you can do it with your own "link" fragment Note that variable names can change but the idea is the same


Here's two ways for navigation item clicks

First Way :- if you want to show fragments after clicking on navigation drawer item, then you can follow some simple steps for this.

  • you have to create a Navigation Graph. for more information Click here

  • You have to give the id of both the menu item and the fragment in Navigation Graph same.

See this image :- Menu Item ID

See this image :- Navigation Graph - Fragment ID

DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    mAppBarConfiguration = new AppBarConfiguration.Builder(
            R.id.home, R.id.gallery, R.id.nav_slideshow)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main3);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

When both id is same, then Your Navigation View will handle your navigation items clicks according to ids.

Second Way :- if you want to show custom things after clicking on navigation drawer item, then you can implement Navigation Item Click Listener.

navigationView.setNavigationItemSelectedListener(item -> {
        if (item.getItemId()==R.id.home){
            // your code
            Toast.makeText(this, "Clicked..", Toast.LENGTH_SHORT).show();
            drawer.close();
            return true;
        }
        return false;
    });

You could follow two approach for this.
First approach
would be to use the setOnMenuItemClickListener when you want to implement listener only for a single item in the navigation drawer. This adds a listener for a single item in the navigation drawer without affecting the other navigation items.

 val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView

    navigationView.menu!!.findItem(R.id.nav_logout).setOnMenuItemClickListener { menuItem:MenuItem? ->
        //write your implementation here
        //to close the navigation drawer
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        }
        Toast.makeText(applicationContext, "single item click listener implemented", Toast.LENGTH_SHORT).show()
        true
    }



Second Approach
would be to use the setNavigationItemSelectedListener when you want to write listener for each item in the navigation drawer.

val navigationView: NavigationView = findViewById(R.id.nav_view) as NavigationView
 navigationView.setNavigationItemSelectedListener { menuItem ->
    when (menuItem.itemId) {
        R.id.nav_gallery -> {
            //write your implementation here
            if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
                drawer_layout.closeDrawer(GravityCompat.START)
            }
            true
        }
        else -> {
            if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
                drawer_layout.closeDrawer(GravityCompat.START)
            }
            false
        }
    }
}