2

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841

3 Answers3

1

If I understood you correctly you want to handle menu clicks. I am not sure if this has to do anything with NavigationDrawer. It does not seems to from the screenshot attached. If so you should implement onOptionsItemSelected to handle whatever action you want to happen, eg:

override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId) {
            R.id.action_settings -> println("handle me!") return true
        }

        return super.onOptionsItemSelected(item)
    }

you need to return either true or false to indicate if menu click was handled by your code (or not).

Filip Zymek
  • 2,626
  • 4
  • 22
  • 31
  • ah, might be, I was typing the code by hand. You might need to reformat it and add `{}` around print and return stamtents – Filip Zymek Jul 01 '20 at 11:40
0

You can override this in your MainActivity, To handle the Menu Item clicks

SAMPLE CODE

override fun onNavigationItemSelected(menuItem: MenuItem): Boolean {
    when (menuItem.itemId) {
        R.id.action_settings-> {
            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show()
            //handle click on settings
        }
        R.id.action_logout-> {
            Toast.makeText(this, "Logout", Toast.LENGTH_SHORT).show()
            //handle click on logout
        }
        
    }
    drawer_layout.closeDrawer(GravityCompat.START) //Closing the drawer
    return true // because you handled the clicks
}
Gouse Mohiddin
  • 420
  • 2
  • 7
0

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

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841