1

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.

2 Answers 2

5

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) 
Sign up to request clarification or add additional context in comments.

4 Comments

where? in OnCreate() as well ?
So I Should add that one line in OnCreate() right? Sorry, It does not work. It returns "Unresolved reference: setNavigationItemSelectedListener". Maybe there's something that i need to change in the activity as well ?
binding.navView.setNavigationItemSelectedListener(this)
Thank you very much sir! Now it is working. I suggest you edit your answer to binding.navView.setNavigationItemSelectedListener(this) so people can see it better
2

I was facing the same issue and I changed my code in this manner to get Toast when Menuitems were select.

  1. I removed NavigationView.OnNavigationItemSelectedListener this from

class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener

  1. created a variable in MainActivity class in MianActivity.kt

private lateinit var navView : NavigationView

  1. assigned navView variable in override fun onCreate function like this by finding navigation view id like this

navView = findViewById(R.id.nav_menu)

which in your case is in activity_main.xml under this Section

            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"/>
  1. Then I implemented this function for onclick select

navView.setNavigationItemSelectedListener {}

I wrote switch case in this function same as your code and now it is working for me fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.