How to Create Option Menu in Android using Kotlin?

04 Oct, 2021
In this article, we will learn how to create an options menu in the Android app using Kotlin. To have an options menu in an Activity, we need to create a new menu XML file and inflate it using menuInflator.inflate( ) method. In menu.xml we will design the options menu as the requirement of the app.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Implement Option Menu
We need to create a new menu XML file and using the <item> tag we can create items inside the menu. 
Step 3: Create vector assets for icons of items in menu options
Refer to this link for Vector Assets. 
Step 4: Refer to this code for the menu.xml file
XML
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">      <item        android:id="@+id/overflowMenu"        android:icon="@drawable/ic_3_dots"        android:title=""        app:showAsAction="always">        <menu>            <item                android:id="@+id/settings"                android:icon="@drawable/ic_settings"                android:title="SETTINGS"                app:showAsAction="never" />            <item                android:id="@+id/about"                android:icon="@drawable/ic_about"                android:title="ABOUT"                app:showAsAction="never" />            <item                android:id="@+id/exit"                android:icon="@drawable/ic_exit"                android:title="EXIT"                app:showAsAction="never" />        </menu>    </item></menu>
Step 5: Working with the MainActivity.kt file
We don’t need to change anything in the activity_main.xml file. Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. 
Kotlin
package com.ayush.optionmenu  import androidx.appcompat.app.AppCompatActivityimport android.os.Bundleimport android.view.Menuimport android.view.MenuItemimport android.widget.Toast  class MainActivity : AppCompatActivity() {    override fun onCreate(savedInstanceState: Bundle?) {        super.onCreate(savedInstanceState)        setContentView(R.layout.activity_main)    }      override fun onCreateOptionsMenu(menu: Menu?): Boolean {        menuInflater.inflate(R.menu.menu,menu)        return super.onCreateOptionsMenu(menu)    }      override fun onOptionsItemSelected(item: MenuItem): Boolean {        when (item.itemId){            R.id.about -> Toast.makeText(this,"About Selected",Toast.LENGTH_SHORT).show()            R.id.settings -> Toast.makeText(this,"Settings Selected",Toast.LENGTH_SHORT).show()            R.id.exit -> Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show()        }        return super.onOptionsItemSelected(item)    }}
So our app is ready. 
Output:
We can see when we click on any menu option a Toast is displayed.
Android
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
How to Communicate Between Fragments in Android?
Retrofit with Kotlin Coroutine in Android
How to Add Views Dynamically and Store Data in Arraylist in Android?
Android UI Layouts
Kotlin Array
How to Communicate Between Fragments in Android?
Retrofit with Kotlin Coroutine in Android