Android jetpack component - toast on navigation drawer click

android navigation drawer with activities
android navigation view
android add navigation drawer to existing activity
drawerlayout
android navigation bar
android navigation component toolbar
android studio navigation bar
appbarconfiguration

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...

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....

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;
     }
 });

How to setup a Drawer with Android Navigation Components, It's been a wile since Android team launched the new Navigation How to setup a Drawer with Android Navigation Components this@MainActivity, "Navigated to $dest", Toast.LENGTH_SHORT //I need to open the drawer onClick Using The Navigation Architecture Component in Android Jetpack� In this article, I will explain how to build the navigation flow of a mobile app and how to pass data between fragments, using the Android Navigation Component. I will also mention some of the…

Use this

 NavigationView navigationView = findViewById(R.id.nav_view);
 navigationView.bringToFront()
 navigationView.setNavigationItemSelectedListener(this);

The New Android In-App Navigation, is a part of Android Jetpack and its main goal is to ease in-app navigation on Android. I'm going to show you how navigation component can simplify your everyday when clicking on adapter item or through navigation drawer. for one time consumable data like displaying SnackBar or Toast message. Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app. Android Jetpack's Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer. The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.

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

Update UI components with NavigationUI, This class contains static methods that manage navigation with the top app bar, the navigation drawer, and bottom navigation. I have made a navigation drawer in Android in which I want to implement onClick for it. This is my main activity: public class MainActivity extends AppCompatActivity { private DrawerLayout

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;
        }
    });

Interact programmatically with the Navigation component, Share UI-related data between destinations with ViewModel. The Navigation component provides ways to programmatically create and interact with certain� Navigation Drawer Android Example Sliding Menu tutorial with the help of PlaceHolderView. Into: Navigation Drawer is the sliding menu that appears on the android screen with a hamburger menu icon in the ActionBar. The construction of it requires placing multiple views inside the navigation portion of the DrawerLayout.

Android Navigation Drawer in Kotlin, Welcome, In this chapter we are focusing on NavigationDrawer, And now, we will create a menu file by right-clicking on the menu and go to makeText(this, " Android Store", Toast. Architecture Components, Coroutines, Unit Testing and much more. Using Jetpack Navigation Component in Android. The Navigation component has a Gradle plugin called Safe Args that generates simple object and builder classes for type-safe navigation and access to any associated arguments. Safe Args is strongly recommended for navigating and passing data, because it ensures type-safety.

Android JetPack - Navigation Architecture, 1 Android JetPack Navigation Architecture Component. 1.1 Principles Right click on the res directory and choose New -> Android resource file. Set the title for � Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write code that works consistently across Android versions and devices so that developers can focus on the code they care about.

Android Jetpack: Navigation Component With Nav Drawer, great video can you please tell how to show a simple toast while clicking on navigation drawer Duration: 14:17 Posted: Jul 17, 2019 The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen. Figure 3. An open drawer displaying a navigation menu. The drawer icon is displayed on all top-level destinations that use a

Comments
  • try debugging it
  • i tired but when i did that...my onNavigationItemSelected(@NonNull MenuItem menuItem) function is never executed although i have done navigationView.setNavigationItemSelectedListener(this); ..
  • 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.
  • @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??
  • 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?