Menu’s in Android

Abhishek Pathak
4 min readFeb 1, 2023

--

Menu is an important UI component which is used to provide various options, features behind an hidden UI functionality around the application.

Types of Menu in android applications

  • Option Menu
  • PopUp Menu
  • Context Menu

Let’s Learn each of the menu one by one

Option Menu : Android Options Menu is a primary collection of menu items in an android application and useful for actions over toolbar for few direct features like settings/logout etc. We have various action mode like visible always, visible if room available etc.

Context Menu : Android Context Menu is a floating menu that only appears when the user clicks for a long time on an element and useful for elements that affect the selected content or context frame. For example over any view like a listview of contacts have options over menu to call/sms etc.

I have a real life example that In Whatsapp — Open your WhatsApp Application and long press on any of the chats you have, saw the change? This is a Context Menu in the action mode.

PopUp Menu : Android Popup Menu displays a list of items in a vertical list which presents to the view that invoked the menu and useful to provide an overflow of actions that related to specific content. It need an view as anchor so below I have a button that acts as anchor to open popUp menu.

Few basic attributes of an menu item

Like any other UI component, even Android menus have also it’s attributes

android: id It uniquely identifies the item of the menu.

android: icon It sets an icon to represent the item.

android: title It sets the title of the item.

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

Let’s go for implementation of Android Menu

Step 1: create a menu directory inside res directory.

Step 2: create menu file by right click on menu dir

Step 3: Implement an menu file by adding items in it.

Step 4: Implement the menu in the kotlin/Java file

for option menu —

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.option_menu, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return return when (item.itemId) {
R.id.share -> {
makeToast("share the message")
true
}
R.id.settings -> {
makeToast("check the setting")
true
}
else -> super.onOptionsItemSelected(item)
}
}

For popUp menu -

private fun popUpMenuWork() {
val popupMenu = PopupMenu(
this,
binding.buttonMenu
) //buttonMenu is an anchor for PopUp menu-> it will create menu
popupMenu.menuInflater.inflate(R.menu.popup_menu, popupMenu.menu)
popupMenu.setOnMenuItemClickListener {
when (it.itemId) {
R.id.mango -> makeToast("mango")
R.id.apple -> makeToast("apple")
R.id.banana -> makeToast("banana")
R.id.grapes -> makeToast("grapes")
R.id.guava -> makeToast("guava")
}
true
}

binding.buttonMenu.setOnClickListener {
popupMenu.show()
}
}
private fun makeToast(message: String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

For Context menu —

private fun contextMenuWork() {
val dataContacts = arrayOf(
"Abhishek +91 9599652867",
"Brian +1 123 444 55",
"Sindura +1 223 453",
"Ruipeng +1 4434 4443",
"jihee +1 32233 3342",
"Hongia +1 32323 23232"
)
val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, dataContacts)
binding.listviewContacts.adapter = arrayAdapter
registerForContextMenu(binding.listviewContacts)
}

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.call -> {
makeToast("call")
true
}
R.id.sms -> {
makeToast("sms")
true
}
else -> super.onContextItemSelected(item)
}
}

Conclusion :

We learn here the types of menu that can be applicable through out any good UI required for enterprise apps.

Thanks for reading this article. Be sure to click 👏 below to applause this article if you found it helpful. It means a lot to me.

--

--