Questions tagged [android-menu]

Questions regarding designing, implementing and handling menus and action bars in Android. A menu is a user accessible list of commands that typically provide additional functionality that may not be core to the generally displayed user interface.

Introduction

Many Android devices (prior to Android 3.0) have a hardware menu button. When the menu button is pressed, a list of additional user commands are made visible for the user to select from. Menu Items allows for both text and icons.

Android 3.0+ devices utilize an on screen menu called the action bar. In contrast to the normal menu, users do not press a hardware menu button to display the options. In fact, many devices using 3.0 have removed the hardware button entirely. In Android, the menu and action bar serve the same purpose. When developed correctly, both may be supported using similar Java code and/or combined Android XML.


Using Android XML

Android XML provides the ability to separate menu design from actual implementation. While this may be tweaked in code, or handled completely with Java, the menu XML is a valuable resource.

Android Menu Handling

There are three primary methods for handling the menu, regardless of how it is designed. These will work on an Android 3.0+ device for supporting the action bar's basic functionality.

  • onCreateOptionsMenu() - This event is fired when the user presses the Menu button for the first time. If the device is Android 3.0+, this will create the Action bar.
  • onPrepareOptionsMenu() - This event is fired when the Menu is actually opened. If this is the first time the button is pressed, it will fire immediately after onCreateOptionsMenu()
  • onOptionsItemSelected() - This event is fired whenever a user chooses a MenuItem from the menu. This event fires in Android 3.0+.
  • onCreateContextMenu()
  • onContextItemSelected()

Action Bar Handling

The native Action Bar utilizes the same methods as the Menu. For extra functionality, the Action Bar also adds the following interfaces to be implemented:

  • TabListener - enables Tabs in the ActionBar
  • OnMenuVisibilityListener - allows objects to respond to Menu visibility
  • OnNavigationListener - allows set up of navigation modes using Action Bar

Additional Links and Information

Development Guides API References Samples
1240 questions
358
votes
26 answers

How do I hide a menu item in the actionbar?

I have an action bar with a menuitem. How can I hide/show that menu item? This is what I'm trying to do: MenuItem item = (MenuItem) findViewById(R.id.addAction); item.setVisible(false); this.invalidateOptionsMenu();
Stir Zoltán
  • 3,871
  • 3
  • 14
  • 14
135
votes
4 answers

Android custom dropdown/popup menu

How do I do a custom dropdown/popup menu anchored to a button? I need it to work like the popup menu (anchored to a view), and do something when I click an item from the menu. How do I add items to the menu by code, keeping menu's height and make…
stanete
  • 3,714
  • 9
  • 19
  • 28
131
votes
9 answers

How to change MenuItem icon in ActionBar programmatically

How to change MenuItem icon in ActionBar programmatically? I tried to use MenuItem menuItem = (MenuItem)findViewById(R.id.action_settings); menuItem.setIcon(getResources().getDrawable(R.drawable.ic_launcher)) but it doesn't work. This is my…
veinhorn
  • 2,206
  • 6
  • 18
  • 32
102
votes
15 answers

PopupWindow - Dismiss when clicked outside

I have a PopupWindow on my activity, the thing is my PopupWindow still shows even when I'm interacting with my activity (say scrolling on my list). I can scroll through my list and the PopupWindow is still there. What I want to achieve is when I'm…
villager
  • 5,041
  • 11
  • 45
  • 101
75
votes
11 answers

Android, How to create option Menu

Here I tried to make option menu, but menu is not displaying on screen, so please guide me where am I doing mistake... MenuTest.java public class MenuTest extends Activity { @Override public boolean onCreateOptionsMenu(Menu menu) { …
vishesh chandra
  • 6,701
  • 6
  • 31
  • 38
74
votes
16 answers

How To show icons in Overflow menu in ActionBar

I know it's not possible using the native API. Is there a workaround to implement that kind of view?
Mihir
  • 2,014
  • 2
  • 17
  • 28
74
votes
8 answers

Setting PopupMenu menu items programmatically

I have a PopupMenu and I know the usual way to associate a menu to it is to use popup.getMenuInflater().inflate(R.menu.my_menu, popup.getMenu()); or something of the like. My problem is, I have an array of items that I want in the menu and I need to…
user766650
72
votes
17 answers

How to change the background color of Action Bar's Option Menu in Android 4.2?

I'd like to change the background color of the option (overflow) menu in Android 4.2. I have tried all the methods but it is still showing the default color set by the theme. I used the following code & XML configs. MainActivity.java public class…
65
votes
9 answers

Android Checkable Menu Item

I have the following menu layout in my Android app:
Icemanind
  • 43,745
  • 45
  • 159
  • 272
64
votes
9 answers

"No resource identifier found for attribute 'showAsAction' in package 'android'"

I am attempting to update my android application to look better for tablets running Honeycomb. As such, I am targeting version 11 of the SDK (my minSdkVersion = 4). This adds the Honeycomb theme to my app, making it look better. However, I have no…
ariets
  • 4,098
  • 4
  • 25
  • 34
64
votes
4 answers

actionlayout on menuitem does nothing

i am setting an actionLayout on a menu item and setting background color and image, but it's not respected. in my activity, i have: getMenuInflater().inflate(R.menu.submit_action, menu); my submit_action is:
tipu
  • 8,864
  • 11
  • 59
  • 96
58
votes
6 answers

How can I dynamically create menu items?

I'm building an Android application and I'm trying to build a user management system where users can login, logout, etc. I want to display a login menu item if the user is logged out and a logout button if the user is logged in. How can I do this…
Johnathan Au
  • 5,061
  • 17
  • 61
  • 120
57
votes
5 answers

Android Split Action Bar with Action Items on the top and bottom?

Is there a way to specify some action items to the top part of the Split Action Bar while the others go to the bottom? Or is it all or nothing, whereby all the action items go to the bottom part of the split only?
Ryan R
  • 7,902
  • 14
  • 74
  • 107
57
votes
3 answers

What is orderInCategory in ActionBar menu item & why it is use for..?

Im working on action menu item and its over flow item this is my main_menu.xml
SaravanaRaja
  • 2,444
  • 1
  • 18
  • 27
55
votes
13 answers

How to change option menu icon in the action bar?

How to change the index icon of option menu? I mean icon (3). Here is my code: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.options, menu); return…
1
2 3
82 83