androidkotlinnavigation-drawerandroid-toolbarmenu-items

OnClickListener for Menu Items in Navigation Drawer Activity


What I am using?
I am writing the default navigation drawer activity by using kotlin

My Question
There is a "three dot dropdown menu" as per image:

enter image description here

Few functions being invoked when i click the menu's. Not sure which piece of code being executed.

My Problem
There are many tutorials out there for Navigation Drawers but I couldn't find anything for the particular case, especially for kotlin.

drawer.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never" />
<item
    android:id="@+id/action_logout"
    android:orderInCategory="100"
    android:title="@string/action_logout"
    app:showAsAction="never" />

Drawer.kt
I do not see any code according to menu action


Solution

  • The 3-dots menu is the overflow menu and is not related to the DrawerLayout.

    In your Activity:

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        val toolbar : Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)
        ...
    }
    

    Then you have to override the onCreateOptionsMenu() to inflate your previously defined menu resource:

       override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.toolbar_menu, menu)
            return 
    

    Finally override the onOptionsItemSelected to handle the click on the item menu:

       override fun onOptionsItemSelected(item: MenuItem): Boolean {
            when(item.itemId) {
                R.id.action_logout -> //....) return true
            }
    
            return super.onOptionsItemSelected(item)
        }
    

    enter image description here enter image description here