Page 1 of 1

Kotlin: Create menu

PostPosted: 26 Dec 2019, 15:19
by Ursego
@ If res/menu directory doesn't exist: right click "res" directory > New > Directory > enter the directory name "menu" > OK.

@ Right click "res/menu" directory > New > File > enter the menu name "XXX_activity_menu.xml" (like "main_activity_menu.xml" if you are creating the menu for MainActivity) > OK > XML > OK > The menu designer opened.

@ Make its text this (the example creates a menu with one item; that item opens the Settings screen, described here; of course, you can customize according to your needs, and add more items):

Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app=""

        app:showAsAction="never" />

@ If you want to change the icon (or add icons to other menu items): switch the menu XML from Text to Design view > in Component Tree, click the menu item > Attributes > icon > click on the vertical line ("Pick a resource").

@ Add to the Activity, for which the menu is created:

Code: Select all
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        (menu as MenuBuilder).setOptionalIconsVisible(true) // remove this line if your menu items don't have icons
        menuInflater.inflate(, menu) // change main_activity_menu to the actual name of your menu if it's different
        return super.onCreateOptionsMenu(menu)
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item?.itemId) {
  <your menu item id> -> {
                val intent = Intent(this, // change PrefActivity to the actual name of the activity the item opens
        return super.onOptionsItemSelected(item!!)


If you want to make a menu item visible/invisible depending on a runtime condition, do the following:

@ In the Activity, which displays the menu, override the function onPrepareOptionsMenu(). In the following example, I show the menu item "Delete Elephant" only if the elephant exists:

Code: Select all
override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
        menu!!.findItem(!!.isVisible = elephantExists()

        return super.onPrepareOptionsMenu(menu)