Home » Android » android – onNavigationItemSelected not calling when item is selected-Exceptionshub

android – onNavigationItemSelected not calling when item is selected-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am adding footer-view in navigation drawer using following code –

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nv_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|start">

        <androidx.core.widget.NestedScrollView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <include layout="@layout/nav_header_main"/>

                <com.google.android.material.navigation.NavigationView
                    android:id="@+id/drawer_menu_body"
                    app:elevation="0dp"
                    android:layout_height="0dp"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    app:menu="@menu/activity_main_drawer">

                </com.google.android.material.navigation.NavigationView>

                <include layout="@layout/navigation_drawer_bottom_view"/>
            </LinearLayout>

        </androidx.core.widget.NestedScrollView>
    </com.google.android.material.navigation.NavigationView>


</androidx.drawerlayout.widget.DrawerLayout>

But now onNavigationItemSelected() is not calling also navigation drawer keeps open until we slide it manually. I am using navigation-component and nav graph.

Side menu with footer view

This is my activity code –

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val toolbar: Toolbar = findViewById(R.id.toolbar)
    setSupportActionBar(toolbar)

    val navController = findNavController(R.id.nav_host_fragment)
    drawer_menu_body.setNavigationItemSelectedListener(this)
    nv_top.setNavigationItemSelectedListener(this)

    appBarConfiguration = AppBarConfiguration(
        setOf(
            R.id.nav_home,
            R.id.nav_gallery,
            R.id.nav_slideshow,
            R.id.nav_tools,
            R.id.nav_share,
            R.id.nav_send
        ), drawer_layout)


    setupActionBarWithNavController(navController, appBarConfiguration)
    drawer_menu_body.setupWithNavController(navController)
    nv_top.setupWithNavController(navController)
}

override fun onNavigationItemSelected(menu: MenuItem): Boolean {
    Log.d("testing_navigation","testing_navigation")
    drawer_layout.closeDrawer(GravityCompat.START)
    return true
}
How to&Answers:

You attach setNavigationItemSelectedListener with NavigationView which will override during default configuration of setupWithNavController. So attach your listener after configuring default settings. Check below code.

nv_top.setupWithNavController(navController)
drawer_menu_body.setNavigationItemSelectedListener(this)

Update: To work with default navigation you have to handle like below:

override fun onNavigationItemSelected(menu: MenuItem): Boolean {
    val handled = NavigationUI.onNavDestinationSelected(menu, navController)

    if (!handled) {
        // handle other navigation other than default
    }

    drawer_layout.closeDrawer(GravityCompat.START)

    return handled
}