Open In App

How to Create Option Menu in Android using Kotlin?

Last Updated : 04 Oct, 2021
Like Article

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 by Step Implementation

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 version="1.0" encoding="utf-8"?>
                app:showAsAction="never" />
                app:showAsAction="never" />
                app:showAsAction="never" />

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. 


package com.ayush.optionmenu
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return super.onCreateOptionsMenu(menu)
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId){
   -> Toast.makeText(this,"About Selected",Toast.LENGTH_SHORT).show()
   -> Toast.makeText(this,"Settings Selected",Toast.LENGTH_SHORT).show()
   -> Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show()
        return super.onOptionsItemSelected(item)

So our app is ready. 


We can see when we click on any menu option a Toast is displayed.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads