Menus and Action Bars (OptionsMenu, ContextMenu) in Android Development
In Android development, menus and action bars play an important role in providing easy access to actions and options for users. Android supports two main types of menus: the Options Menu and the Context Menu. The action bar also provides a place for displaying menus. This article covers the creation and handling of both Options Menus and Context Menus using Kotlin in Android.
1. Options Menu
The Options Menu is the primary menu for an activity, and it is usually represented as an icon (three dots) in the action bar. This menu is typically used for global actions, such as saving, sharing, or refreshing.
Example 1: Creating and Handling Options Menu
    class MainActivity : AppCompatActivity() {
        override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            // Inflate the menu into the action bar
            menuInflater.inflate(R.menu.main_menu, menu)
            return true
        }
        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            return when (item.itemId) {
                R.id.action_save -> {
                    Toast.makeText(this, "Save option clicked", Toast.LENGTH_SHORT).show()
                    true
                }
                R.id.action_share -> {
                    Toast.makeText(this, "Share option clicked", Toast.LENGTH_SHORT).show()
                    true
                }
                else -> super.onOptionsItemSelected(item)
            }
        }
    }
        
        In this example:
- The onCreateOptionsMenu()method is overridden to inflate the options menu from a resource file (R.menu.main_menu).
- The onOptionsItemSelected()method handles the selection of menu items, usingwhento check for specific item IDs and display correspondingToastmessages.
Menu Resource File: res/menu/main_menu.xml
        
    
        
        In the main_menu.xml file:
- android:iddefines the unique ID for the menu item.
- android:titlespecifies the text that will be displayed for the menu item.
- android:icondefines the icon for the menu item.
- android:showAsActiondetermines when the item should appear in the action bar (e.g.,- ifRoommakes it appear when there is enough space).
2. Context Menu
The Context Menu is a floating menu that appears when the user long-presses on a UI element, such as a list item or a button. It is typically used for contextual actions that apply to a specific item or view.
Example 2: Creating and Handling Context Menu
    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            val myView = findViewById(R.id.myTextView)
            registerForContextMenu(myView)
        }
        override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
            super.onCreateContextMenu(menu, v, menuInfo)
            menuInflater.inflate(R.menu.context_menu, menu)
        }
        override fun onContextItemSelected(item: MenuItem): Boolean {
            return when (item.itemId) {
                R.id.context_edit -> {
                    Toast.makeText(this, "Edit option clicked", Toast.LENGTH_SHORT).show()
                    true
                }
                R.id.context_delete -> {
                    Toast.makeText(this, "Delete option clicked", Toast.LENGTH_SHORT).show()
                    true
                }
                else -> super.onContextItemSelected(item)
            }
        }
    }
         
        In this example:
- registerForContextMenu()is called to register a- View(in this case, a- TextView) for showing a context menu on long press.
- The onCreateContextMenu()method is overridden to inflate the context menu from a resource file (R.menu.context_menu).
- The onContextItemSelected()method handles the selection of items from the context menu, similarly to how options menu items are handled.
Context Menu Resource File: res/menu/context_menu.xml
        
    
        
        In the context_menu.xml file:
- android:iddefines the unique ID for each menu item.
- android:titlespecifies the text for the context menu item.
- android:icondefines the icon for each context menu item.
3. Overflow Menu
In some cases, you may want to add an overflow menu to the action bar for additional actions that do not fit in the main options menu. The overflow menu is accessible via the three vertical dots in the action bar.
Example 3: Adding an Overflow Menu
    class MainActivity : AppCompatActivity() {
        override fun onCreateOptionsMenu(menu: Menu?): Boolean {
            menuInflater.inflate(R.menu.main_menu, menu)
            return true
        }
        override fun onOptionsItemSelected(item: MenuItem): Boolean {
            return when (item.itemId) {
                R.id.action_settings -> {
                    Toast.makeText(this, "Settings option clicked", Toast.LENGTH_SHORT).show()
                    true
                }
                else -> super.onOptionsItemSelected(item)
            }
        }
    }
        
        Overflow Menu Resource File: res/menu/main_menu.xml
        
    
        
        In this example:
- The android:showAsAction="never"ensures the item will appear in the overflow menu, not in the action bar.
4. Conclusion
Menus and action bars are fundamental for providing users with easy access to actions and options in Android applications. By using the Options Menu, Context Menu, and overflow menu, you can offer users a rich and interactive experience. Handling these menus in Kotlin is straightforward, allowing you to easily implement the most common actions and workflows in your apps.