Open In App

How to Create Option Menu in Android using Kotlin?

Last Updated : 04 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




<?xml version="1.0" encoding="utf-8"?>
  
    <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.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import 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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads