Skip to content

Android Menu – Options Menu Tutorial and Example in Kotlin

Android Menu provides a user interface in Android applications. The concept of the Android menu has come to forgive a user experience to the user. Like if you have the same option (action button ) in multiple activities, then you have to add a menu in the App bar for a good user interface experience.

Android Menu example

In this tutorial, you will learn the 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 places 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 the Gmail app you have to long-press any email then the Context menu appears with options 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 recognizes 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 an Example of the Android Options 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

<?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/action_search"
        android:icon="@mipmap/outline_search_white_18dp"
        android:title="Search"
        app:showAsAction="always" />
    <item
        android:id="@+id/action_profile"
        android:title="Profile" />
    <item
        android:id="@+id/action_setting"
        android:title="Setting" />
</menu>
Step 5. Open the “MainActivity.kt” and add following code
package `in`.eyehunt.androidmenu

import android.support.v7.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 {
        val inflater = menuInflater
        inflater.inflate(R.menu.manu, menu)
        return true
    }

    // actions on click menu items
    override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
        R.id.action_search -> {
            msgShow("Search")
            true
        }
        R.id.action_profile -> {
            msgShow("Profile")
            true
        }
        R.id.action_setting -> {
            msgShow("Setting")
            true
        }
        else -> {
            // If we got here, the user's action was not recognized.
            // Invoke the superclass to handle it.
            super.onOptionsItemSelected(item)
        }
    }

    fun msgShow(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
    }
}
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

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading