Reputation: 133
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:
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
Upvotes: 2
Views: 2056
Reputation: 365028
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)
}
Upvotes: 0
Reputation: 424
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
}
Upvotes: 0
Reputation: 2646
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).
Upvotes: 1