Questions tagged [android-optionsmenu]

Used to display the options of a application on android devices, this can be at the bottom of the application when the menu-button is pressed or in the overflow-menu of the actionbar.

Introduction
Menus are a common user interface component in many types of applications. To provide a familiar and consistent user experience, you should use the Menu APIs to present user actions and other options in your activities.

Beginning with Android 3.0 (API level 11), Android-powered devices are no longer required to provide a dedicated Menu button. With this change, Android apps should migrate away from a dependence on the traditional 6-item menu panel and instead provide an action bar to present common user actions.

Although the design and user experience for some menu items have changed, the semantics to define a set of actions and options is still based on the Menu APIs. This guide shows how to create the three fundamental types of menus or action presentations on all versions of Android:

Options menu and action bar
The options menu is the primary collection of menu items for an activity. It's where you should place actions that have a global impact on the app, such as "Search," "Compose email," and "Settings."

If you're developing for Android 2.3 or lower, users can reveal the options menu panel by pressing the Menu button.

On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.

The options menu
The options menu is where you should include actions and other options that are relevant to the current activity context, such as "Search," "Compose email," and "Settings."

Where the items in your options menu appear on the screen depends on the version for which you've developed your application:

  • If you've developed your application for Android 2.3.x (API level 10) or lower, the contents of your options menu appear at the bottom of the screen when the user presses the Menu button, as shown in figure 1. When opened, the first visible portion is the icon menu, which holds up to six menu items. If your menu includes more than six items, Android places the sixth item and the rest into the overflow menu, which the user can open by selecting More.

  • If you've developed your application for Android 3.0 (API level 11) and higher, items from the options menu are available in the action bar. By default, the system places all items in the action overflow, which the user can reveal with the action overflow icon on the right side of the action bar (or by pressing the device Menu button, if available). To enable quick access to important actions, you can promote a few items to appear in the action bar by adding android:showAsAction="ifRoom" to the corresponding elements (see image below).

Action bar on android 3.0+
Action bar on android 3.0+ with menu button on the right

Action bar on android 2.3 http://developer.android.com/images/options_menu.png
Options menu on android 2.3 or lower

According to the official Android Developers website

365 questions
457
votes
25 answers

How to add Options Menu to Fragment in Android

I am trying to add an item to the options menu from a group of fragments. I have created a new MenuFragment class and extended this for the fragments I wish to include the menu item in. Here is the code: Java: public class MenuFragment extends…
misterbassman
  • 4,589
  • 2
  • 14
  • 4
72
votes
17 answers

Hide MenuItem in some Fragments

I using menu drawer which has more Fragments. In some Fragments I have menu item REFRESH but in some fragments I want hide this menu item (I don't want show menu but I don't want hide ActionBar). I try add override onCreateOptionsMenu() to Fragment…
pavol.franek
  • 1,396
  • 3
  • 19
  • 42
64
votes
3 answers

How to add Action bar options menu in Android Fragments

I am trying to have a options menu in Android Fragments. ActionBar options menu are not displaying in my Fragments. Here is my code and I have both onCreateOptionsMenu() and onOptionSelected() function. My code doesn't shows any error. But options…
57
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…
47
votes
9 answers

Android - Correct use of invalidateOptionsMenu()

I have been searching a lot on invalidateOptionsMenu() and I know what it does. But I cannot think of any real life example where this method could be useful. I mean, for instance, let's say we want to add a new MenuItem to our ActionBar, we can…
38
votes
2 answers

onCreateOptionsMenu() calling super

I'm creating application with OptionsMenu. I found few examples with it, but everyone is using different place where to call super.onCreateOptionMenu() in onCreateOptionsMenu() method. List of different ways: @Override // without super public…
skywall
  • 3,956
  • 1
  • 34
  • 52
33
votes
5 answers

How to add toggle button in menu item in android

I have options menu item in my application. Requirement was to add a toggle button to a menu item. Is this possible?
Anju
  • 9,379
  • 14
  • 55
  • 94
32
votes
2 answers

How can I use onCreateOptionsMenu in a Fragment within a tab Activity? (Sherlock ActionBar)

I have the following method overridden in my tab Activity and it works fine like that but I want specific option menu's for each fragment. When I put this in my fragment and press the menu button, nothing happens. @Override public void…
29
votes
11 answers

Android: java.lang.IllegalArgumentException: Invalid payload item type

Some users tell me about the exception the got: java.lang.IllegalArgumentException: Invalid payload item type at android.util.EventLog.writeEvent(Native Method) at android.app.Activity.onMenuItemSelected(Activity.java:2452) at…
25
votes
1 answer

How to change Toolbar navigation icon and options menu margin

Is there any way to change the left margin of the Navigation icon and the right margin of the Action items in Toolbar?
timemanx
  • 4,493
  • 6
  • 34
  • 43
22
votes
3 answers

Options menu not showing in ICS using compatibility library

I can't get an options menu to show in a Fragment in ICS in a project which uses the android-support-v4.jar library. I'm testing on a Galaxy Nexus handset. We aren't using the action bar, and need the app to be 2.2+ compatible. We aren't seeing…
18
votes
2 answers

Pressing menu button causes crash in Activity with no ActionBar

I'm a newbie in Android and working on my first app. I have the main activity with no ActionBar in it. And I don't want to display any menu in that Activity. Everything is working just fine But when I press the menu button present in the device…
Anjani
  • 1,533
  • 3
  • 15
  • 26
18
votes
1 answer

Warning when canceling ActionBar Overflow menu on Android 4.1.x

This is the warning I'm getting: 03-02 14:38:43.980: W/InputEventReceiver(3961): Attempted to finish an input event but the input event receiver has already been disposed. The menu I have was generated from a regular res/menu/activity_menu.xml…
alexismorin
  • 787
  • 1
  • 6
  • 21
17
votes
3 answers

Android options menu icon won't display

I'm following a book on Android Development to get myself started writing my first real app. I got up to the point where I'm making an options menu for one of my activities. The menu shows up, but the corresponding icon of the menu item refuses to…
Jort
  • 1,401
  • 8
  • 23
  • 39
17
votes
4 answers

Toolbar options menu background color

I am using the toolbar for android. I just want to change the background color of the overflow menu. But it is not changing. Style xml