Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation view items will not respond when pressed

I am working on an app with a side Navigation drawer. The drawer opens fine, however the text that supposedly can be "clickable" does not seem to respond. The animation shows that there is feedback to when the drawer is tapped (you can hear the sound) however nothing results of it. I have tried to place toast messages to see if the button registers an action, but when pressed, no toast appears. The code goes as follows (I have implemented NavigationView.OnNavigationItemSelectedListener):

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_driver_home);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        // 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.nav_home, R.id.nav_history, R.id.nav_settings,
                R.id.nav_help, R.id.nav_signout)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

And then I implemented the method:

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        switch (menuItem.getItemId()){
            case R.id.nav_history:
    Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
                break;
            case R.id.nav_help:

                break;
            case R.id.nav_settings:

                break;
            case R.id.nav_signout:
                signOut();
                break;
        }

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

Thank you

like image 852
Stradtdog Avatar asked Dec 14 '22 09:12

Stradtdog


2 Answers

The line

NavigationUI.setupWithNavController(navigationView, navController);

Calls setNavigationItemSelectedListener internally to connect destinations to menu items (i.e., when you click on the R.id.nav_settings MenuItem, it'll replace the Fragment in your NavHostFragment with the one with android:id="@+id/nav_settings" set). This listener overrides the OnNavigationItemSelectedListener view you've set, which is why your custom logic doesn't run.

If you want to combine both sets of functionality together, you need to call navigationView.setNavigationItemSelectedListener(this); after setupWithNavController and trigger the default behavior with NavigationUI.onNavDestinationSelected():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_driver_home);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view);
    // 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.nav_home, R.id.nav_history, R.id.nav_settings,
            R.id.nav_help, R.id.nav_signout)
            .setDrawerLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);
    // This line needs to be after setupWithNavController()
    navigationView.setNavigationItemSelectedListener(this);

}

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
    switch (menuItem.getItemId()){
        case R.id.nav_history:
            Toast.makeText(this, "fsdfuxc", Toast.LENGTH_LONG).show();
            break;
        case R.id.nav_signout:
            signOut();
            break;
        default:
            // Trigger the default action of replacing the current
            // screen with the one matching the MenuItem's ID
            NavigationUI.onNavDestinationSelected(menuItem, navController);
    }

    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
like image 173
ianhanniballake Avatar answered Dec 28 '22 10:12

ianhanniballake


Just a small sample based on the accepted answer but inside the fragment, without overriding the annotation and using Kotlin.

 bottomNav.setOnNavigationItemSelectedListener {

        when (it.itemId) {
            R.id.share -> {
                shareViaWhatsApp()
            }
            else -> {
                NavigationUI.onNavDestinationSelected(it, navController!!)
            }
        }

        true

    }
like image 25
Francislainy Campos Avatar answered Dec 28 '22 09:12

Francislainy Campos