Kotlin Android Navigation Drawer item onclick listener does not working

Issue

I know this already asked many times, but in my case, I still don’t get it.

So I have a navigation drawer that set up in the MainActivity.kt (My app has many fragment that runs on this Activity).

MainActivity.kt

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    lateinit var drawerLayout: DrawerLayout
    lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        drawerLayout = binding.drawerLayout

        //Get navigation controller of this App
        val navController = this.findNavController(R.id.myNavHostFragment)

        //Lock the Nav drawer
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)

        NavigationUI.setupActionBarWithNavController(this, navController, drawerLayout)
        NavigationUI.setupWithNavController(binding.navView, navController)
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.nav_logout -> {
                Toast.makeText(this, "Publication", Toast.LENGTH_SHORT).show()
                Log.i("MainActivity", "Sign out clicked!")
            }
            R.id.nav_messages -> {
                Toast.makeText(this, "Publication", Toast.LENGTH_SHORT).show()
            }
        }    

        binding.drawerLayout.closeDrawer(GravityCompat.START)
        return true
    }

    // Set up the back button on action bar
    override fun onSupportNavigateUp(): Boolean {
        val navController = this.findNavController(R.id.myNavHostFragment)

        return NavigationUI.navigateUp(navController, drawerLayout)
    }
}

This is my Activity_main.xml

Activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.drawerlayout.widget.DrawerLayout
        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">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/myNavHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                app:navGraph = "@navigation/navigation"
                app:defaultNavHost = "true"
                />

        </LinearLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header"
            app:menu="@menu/nav_drawer_menu"/>

    </androidx.drawerlayout.widget.DrawerLayout>

</layout>

This is the menu layout for the navigation drawer:
nav_drawer_menu.xml

<?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group>
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_person"
            android:title="Profile"/>
        <item
            android:id="@+id/nav_messages"
            android:icon="@drawable/ic_people"
            android:title="Messages"/>
    </group>


    <item android:title="Account Settings">
        <menu>
            <item
                android:id="@+id/nav_logout"
                android:title="Sign Out"/>
        </menu>
    </item>
</menu>

This is how the navigation drawer looks like.

Am I missing something ? If there’s anything unclear let me know.

Solution

You should use setNavigationItemSelectedListener.

Set a listener that will be notified when a menu item is selected.

 navigationViewOBJ.setNavigationItemSelectedListener(this) 

For DataBinding You should use

 binding.navView.setNavigationItemSelectedListener(this) 

Answered By – IntelliJ Amiya

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published