Adding ActionMenuView on Toolbar
ActionMenuView is a presentation of a series of menu options as a View. It provides several top level options as action buttons while spilling remaining options over as items in an overflow menu.
How to implement it on a Toolbar?
1. Add a Toolbar as app bar. See this.
2. Add each action button icon (drawable resource).
3. Create a menu resource file. Each menu item will be an action button. The tag app:showAsAction defines whether the action button will be showed if window have space to show it (app:showAsAction="ifRoom") or whether it only will be showed as an option on the overflow menu (app:showAsAction="never").
4. Inflate (Add) the menu to the toolbar. We inflate the menu on activity's method onCreateOptionsMenu. See the example below.
override fun onCreateOptionsMenu(menu: Menu): Boolean {5. Handle clicks on action button on activity's method onOptionsItemSelected. See the example below.
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.game_menu, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
return when (item.itemId) {
R.id.new_game -> {
newGame()
true
}
R.id.help -> {
showHelp()
true
}
else -> super.onOptionsItemSelected(item)
}
}
