Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation drawer item click listener not working

Sorry for the silly question i am amateur in android studio and learning now. I have tried a lot but the click listener is not working please help. i have used the android studio's default drawer layout.navigation is working but i want to perform a special action such as using a new intent to open another app.I am trying to use it on the ID nav_link to perform a simple toast but its not working.

package com.demo.navdraw;
import android.content.ClipData;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import android.widget.Toast;



import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

private AppBarConfiguration mAppBarConfiguration;
private MenuItem item;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.nav_link) {
        Toast.makeText(this, "Setting", Toast.LENGTH_LONG).show();
        return true;
    }

    return super.onOptionsItemSelected(item);
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar=findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    FloatingActionButton fab=findViewById(R.id.fab);



    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });



    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_gallery, R.id.nav_slideshow,
            R.id.nav_tools, R.id.nav_share, R.id.nav_send, R.id.nav_profile, R.id.nav_link)
            .setDrawerLayout(drawer)
            .build();
    NavController navController=Navigation.findNavController(this, R.id.nav_host_fragment);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);


}

@Override
public boolean onSupportNavigateUp() {
    NavController navController=Navigation.findNavController(this, R.id.nav_host_fragment);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem Item) {
    int id=item.getItemId();

    if (id==R.id.nav_link){

            Toast.makeText(getApplicationContext(), "Link", Toast.LENGTH_LONG).show();
            return true;

        }
    return true;
    }


}
like image 827
Honey samresH Avatar asked Nov 03 '19 12:11

Honey samresH


People also ask

What is DrawerLayout in Android?

DrawerLayout acts as a top-level container for window content that allows for interactive "drawer" views to be pulled out from one or both vertical edges of the window.


2 Answers

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

like image 61
Ariel Avatar answered Oct 19 '22 20:10

Ariel


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
        }
    }
}
like image 20
akshayashok Avatar answered Oct 19 '22 20:10

akshayashok