Android Menu – Options Menu Tutorial and Example in Kotlin

Android Menu is provide a user interface in Android application. The concept of Android menu has came for give a user experience to user. Like if your have same option (action button ) in multiple activity , then you have to add menu in the App bar for good user interface experience.

Android Menu example

In this tutorial, you will learn following:

  1. Overview Android Manu and Option Menu 
  2. Example and code of Option Menu

Types of Android Menus :

  • Options menu: is the collection of menu items for an activity. Where app activity place actions that have a global impact on the app, such as “Profile,” “Login/Logout”, “Notification” and “Settings.”
  • Context menu and contextual action mode:  A context menu appears when the user performs a long-click on an element. That provides actions that affect the selected content or context frame. Like in Gmail app you have to long-press any email then Context menu appears with option Delete, Archived, etc. The context menu allows the user to select multiple items.
  • Popup menu: A popup menu displays a list of items in a vertical list that’s anchored to the view that invoked the menu

Android Menu Elements 

<menu> : It’s a container of menu Items. A <menu> element is the root node for the file and it hold one or more <item> and <group> elements.

<item>  : MenuItem is represents a single item in a menu. This element may contain a nested <menu>element in order to create a submenu.

<group> This is an optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and visibility.

Android Menu Attributes 

android:id A resource ID that’s unique to the item, Wheres the application to recognize the item when the user selects it.

android:iconA reference to a drawable to use as the item’s icon

.android:titleA reference to a string to use as the item’s title.

android:showAsActionSpecifies when and how this item should appear as an action item in the app bar.

Let’s Build a Example of Android Option Menu :

Step 1. Create new project “Build Your First Android App in Kotlin
Step 2. Create “new Resource Directory” if menu folder not showing in project

Then create a new menu Directory , if you already have menu directory in project you can skip this step.

Android Menu - Option Menu Tutorial and Example
Step 3. Create “menu.xml” in menu directory
Android Menu - Option Menu Tutorial and Example menu
Step 4. open a “menu.xml” and this code
Android Menu - Option Menu Tutorial

This code for menu item will show in activity.

Before copy code add this image in mipmap folder

outline_search_white_18dp.png

Step 5. Open the “MainActivity.kt” and add following code
Step 6. Now Run the application, in emulator or On you android device

Output screenshot Android Menu example :

Android Menu - Option Menu Tutorial and Example in Kotlin ouput

Download source code Android Menu in kotlin

https://github.com/eyehunt/AndroidMenu

Do comment if you have any doubt and suggestion on this tutorial.

Note: This example (Project) is developed in Android Studio 3.1.3. Tested on Android 9 ( Android-P), compile SDK version API 27: Android 8.0 (Oreo)

MinSdkVersion=”15″

TargetSdkVersion=”27″

Coding in Kotlin


One thought on “Android Menu – Options Menu Tutorial and Example in Kotlin

  1. will be wonderfull if you share or can give me some support in order to do it in multiple activities in kotlin language.
    Thank you!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.