2

I am using navigation jetpack and have set up navigation drawer. Every thing works fine. But the problem is I want to show a toast when user clicks "nav_share" but it is not showing... my nav drawer..i want to show toast when user clicks order history whose id is "nav_share"

here is how i made navigation drawer

DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);

        navigationView.setNavigationItemSelectedListener(this);

        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_plan, R.id.navigation_notifications)
                .setDrawerLayout(drawer)
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);

my menu for navigation drawer is

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
        <item
            android:id="@+id/nav_tools"
            android:icon="@drawable/ic_menu_manage"
            android:title="@string/menu_tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="@string/menu_share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="@string/menu_send" />
        </menu>
    </item>

</menu>

finally:

@Override
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

        int id = menuItem.getItemId();

        if (id == R.id.nav_share)
            Toast.makeText(LauncherActivity.this, "Click", Toast.LENGTH_SHORT).show();

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

i want that click toast...i cannot see what am i missing....

Prashant Sable
  • 1,003
  • 7
  • 19
unownsp
  • 727
  • 5
  • 19
  • try debugging it – Amanpreet Kaur Nov 21 '19 at 09:55
  • i tired but when i did that...my onNavigationItemSelected(@NonNull MenuItem menuItem) function is never executed although i have done navigationView.setNavigationItemSelectedListener(this); .. – unownsp Nov 21 '19 at 09:58
  • 1
    The `NavigationUI.setupWithNavController()` call is calling `setNavigationItemSelectedListener()` itself internally, which overrides the call you made earlier. Your `onNavigationItemSelected()` is not going to be called, since the Navigation framework is handling the `NavigationView` clicks. – Mike M. Nov 21 '19 at 10:20
  • @MikeM. if I add navigationView.setNavigationItemSelectedListener(this); on last of my onCreate() then the toast is shown...but now i cannot navigate to my dashboard, plans or profile .... how can i show the toast and be able to navigate between my fragments..can you elaborate?? – unownsp Nov 21 '19 at 10:26
  • 1
    Right, because then your call is overriding the one that `NavigationUI.setupWithNavController()` sets. Why do you want to show a `Toast` there? Surely that's not what you ultimately want to do, yeah? – Mike M. Nov 21 '19 at 10:30
  • yeah,i will be adding logout option there..where i need to add a alert dialog to confirm logout...so i am testing with toast so later i can implement a alert dialog – unownsp Nov 21 '19 at 10:32
  • Well, it'd probably be less work, in the end, to just go ahead and set up a stub dialog destination. Otherwise, I _think_ you can set your `OnNavigationItemSelectedListener`, and call [`NavigationUI.onNavDestinationSelected()`](https://developer.android.com/reference/androidx/navigation/ui/NavigationUI.html#onNavDestinationSelected(android.view.MenuItem,%20androidx.navigation.NavController)) in `onNavigationItemSelected()`, if `id != R.id.nav_share`. I'm not very familiar with the Navigation framework, yet, but that seems to be the way to do it, AFAICT. – Mike M. Nov 21 '19 at 10:45

5 Answers5

3

if any one wants the answer..i did some research and finally found a solution to it...hope it helps....

NavigationView navigationView = findViewById(R.id.nav_view);
MenuItem shareItem = navigationView.getMenu().findItem(R.id.nav_share);
shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
     @Override
     public boolean onMenuItemClick(MenuItem item) {

     Toast.makeText(LauncherActivity.this, "click", Toast.LENGTH_SHORT).show();
     //do as you want with the button click

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

      return true;
     }
 });
unownsp
  • 727
  • 5
  • 19
0

Use this

 NavigationView navigationView = findViewById(R.id.nav_view);
 navigationView.bringToFront()
 navigationView.setNavigationItemSelectedListener(this);
mehul chauhan
  • 1,792
  • 11
  • 26
0

I also had your problem and came up with the solution

NavController navController = Navigation.findNavController(this, R.id.nav_host_home);
    NavigationUI.setupWithNavController(navigationView, navController);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            int id = menuItem.getItemId();
            if (id == R.id.nav_share) {
                Toast.makeText(getApplicationContext(), "nav_share", Toast.LENGTH_SHORT).show();
            }
            NavigationUI.onNavDestinationSelected(menuItem, navController);
            drawerLayout.closeDrawer(Gravity.RIGHT);
            return true;
        }
    });
0

In case if anyone is still searching for the answer as to how we can tie up navigation destinations and also handle click on menu items of the navigation drawer, it has been answered here: https://stackoverflow.com/a/57846680/3283350

ravi
  • 899
  • 8
  • 31
0

Android Jetpack Navigation - Custom Action with Drawer Item

Simple step and easy

class MainActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener{

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val navView: NavigationView = binding.navView
    navController = findNavController(R.id.nav_host_fragment_content_main)
    navView.setNavigationItemSelectedListener(this)

 }

 override fun onNavigationItemSelected(item: MenuItem): Boolean {

    val url: String? = when (item.itemId) {
        R.id.nav_privacy_policy -> MenuItemUrl.PRIVACY_POLICY
        R.id.nav_term_condition -> MenuItemUrl.TERM_AND_CONDITION
        R.id.nav_contact_us -> MenuItemUrl.CONTACT_US
        R.id.nav_feedback -> MenuItemUrl.FEEDBACK
        R.id.nav_help_support -> MenuItemUrl.HELP_AND_SUPPORT
        R.id.nav_faq -> MenuItemUrl.FAQ
        R.id.nav_about_us -> MenuItemUrl.ABOUT_US
        else -> null
    }

    if (url != null) Methods.openBrowser(this@MainActivity, url) // Do anything here ex. show toast etc.
    NavigationUI.onNavDestinationSelected(item, navController)
    drawerLayout.closeDrawer(GravityCompat.START)
    return true
 }

}
Kumar Santanu
  • 603
  • 1
  • 7
  • 14