How to Create Option Menu in Android using Kotlin?
Last Updated :
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 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"?>
<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.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.
Similar Reads
How to Create Options Menu for RecyclerView in Android using Kotlin? RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item that can be custom
6 min read
How to Send SMS in Android using Kotlin? SMS Manager is a class in Android which is used to send the SMS to a specific contact from the android application. We can send text messages, data messages, and multimedia messages using this class. There are different methods that are provided to send different types of messages. In this article,
4 min read
How to Send Data Back to MainActivity in Android using Kotlin? As there are many methods to send the data, but in this article, we will use startActivityForResult() method. Here we need to launch a child activity using startActivityForResult() method. Then from child activity, we can easily send data back to Main Activity. Example: Note: To implement it in jav
2 min read
How to Build a Simple Torch App in Android using Kotlin? Torch Application is a very basic application that every beginner level android developer should definitely try to build while learning Android. In this article, we will be creating an application in which we will simply display a toggle button to switch on and switch off the torch. Note: If you are
4 min read
Bottom Navigation Bar in Android Using Kotlin We all have come across apps that have a Bottom Navigation Bar. Some popular examples include Instagram, WhatsApp, etc. In this article, we will learn about bottom navigation using Fragments. We will learn it by making a project in android studio. Here we will be using Kotlin as the language for dev
4 min read
How to Create Interfaces in Android Studio? Interfaces are a collection of constants, methods(abstract, static, and default), and nested types. All the methods of the interface need to be defined in the class. The interface is like a Class. The interface keyword is used to declare an interface. public interface AdapterCallBackListener { void
4 min read