Kotlin Tutoial
Basics
Control Flow
Array & String
Functions
Collections
OOPs Concept
Exception Handling
Null Safety
Regex & Ranges
Java Interoperability
Miscellaneous
Android
Menus are a common feature in Android apps. They provide a user interface component that presents app-related actions as well as navigation options. Android provides a flexible way to add and customize menus.
In this tutorial, we'll go over creating a basic options menu using Kotlin in an Android app.
Prerequisites:
1. Set up a new project:
Open Android Studio and create a new project. Choose an empty activity template.
2. Defining the menu:
menu
inside the res
folder.menu
directory, create an XML file named main_menu.xml
.<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_settings" android:title="Settings" android:orderInCategory="100" android:showAsAction="never" /> <item android:id="@+id/action_about" android:title="About" android:orderInCategory="101" android:showAsAction="never" /> </menu>
3. Inflate the menu in your Activity:
Open your MainActivity.kt
:
override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.main_menu, menu) return true }
4. Handle menu item clicks:
Still in MainActivity.kt
, override onOptionsItemSelected
:
override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.action_settings -> { Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show() return true } R.id.action_about -> { Toast.makeText(this, "About selected", Toast.LENGTH_SHORT).show() return true } else -> return super.onOptionsItemSelected(item) } }
5. Running the App:
When you run your app and click on the three vertical dots (or the physical menu button on some devices) on the top right corner of the app, you should see the "Settings" and "About" options. When you click on them, a Toast message will appear.
6. Additional Tips:
android:showAsAction
determines how the item will show. If you set it to "always"
, it will always show as a direct action in the UI. "never"
means it'll be hidden under the menu.<group>
elements, use icons, and even create sub-menus.Menus are an integral part of Android apps, and with Kotlin, the process is more streamlined and expressive. Use them wisely to improve the navigation and functionality of your apps.
Description:
Creating an options menu in Android involves overriding the onCreateOptionsMenu
method and inflating a menu resource.
Code (Activity class):
class MainActivity : AppCompatActivity() { override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.options_menu, menu) return true } // Other methods }
Description:
Contextual menus are displayed when users long-press on a view. Override onCreateContextMenu
to define the menu items.
Code (Activity class):
class MainActivity : AppCompatActivity() { override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInfo: ContextMenu.ContextMenuInfo?) { super.onCreateContextMenu(menu, v, menuInfo) menuInflater.inflate(R.menu.context_menu, menu) } // Other methods }
Description:
Handle menu item clicks by overriding the onOptionsItemSelected
method in your activity.
Code (Activity class):
class MainActivity : AppCompatActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_item1 -> { // Handle item1 click return true } R.id.menu_item2 -> { // Handle item2 click return true } // Other menu items else -> return super.onOptionsItemSelected(item) } } // Other methods }
Description: Include icons with menu items by specifying them in the menu resource XML file.
Code (menu resource XML):
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item1" android:icon="@drawable/ic_menu_item1" android:title="Item 1" app:showAsAction="ifRoom" /> <!-- Other menu items --> </menu>
Description: PopupMenu is a convenient way to display a list of items anchored to a view.
Code (Activity class):
class MainActivity : AppCompatActivity() { private fun showPopupMenu(view: View) { val popupMenu = PopupMenu(this, view) popupMenu.menuInflater.inflate(R.menu.popup_menu, popupMenu.menu) popupMenu.setOnMenuItemClickListener { item -> when (item.itemId) { R.id.popup_item1 -> { // Handle popup_item1 click true } R.id.popup_item2 -> { // Handle popup_item2 click true } else -> false } } popupMenu.show() } // Other methods }
Description: Customize menus by defining a custom layout for menu items or applying styles.
Code (menu resource XML with custom layout):
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item_custom" android:title="" app:actionLayout="@layout/custom_menu_item_layout" app:showAsAction="always" /> <!-- Other menu items --> </menu>
Description: The overflow menu appears when there is not enough space to display all items in the action bar.
Code (menu resource XML with overflow menu):
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_item1" android:title="Item 1" app:showAsAction="ifRoom" /> <!-- Other menu items --> <item android:id="@+id/menu_overflow" android:icon="@drawable/ic_overflow" android:title="Overflow" app:showAsAction="always"> <menu> <item android:id="@+id/overflow_item1" android:title="Overflow Item 1" /> <!-- Other overflow items --> </menu> </item> </menu>
Description: ActionMode provides a contextual action bar for performing actions on selected items.
Code (Activity class):
class MainActivity : AppCompatActivity() { private var actionMode: ActionMode? = null override fun onContextItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.context_menu_item_delete -> { // Handle delete action return true } // Other context menu items else -> return super.onContextItemSelected(item) } } private val actionModeCallback = object : ActionMode.Callback { override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean { mode.menuInflater.inflate(R.menu.context_menu, menu) return true } override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean { // Update action mode UI if needed return false } override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { // Handle action item click return true } override fun onDestroyActionMode(mode: ActionMode) { // Cleanup and reset UI actionMode = null } } // Other methods }