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
1268 questions
15
votes
5 answers

How change position of popup menu on android overflow button?

I just like to implement somethings same as popup menu in the Gmail app, anchored to the overflow button at the top-right. for that I used the same code as google tutorial for android Android popup menu, but for me show pop menu on top of edge of…
user3266062
  • 522
  • 1
  • 7
  • 21
14
votes
5 answers

Navigation Drawer menu items selected within different groups

I have a working Navigation Drawer and having some issues with menuItem.setChecked(true); when using groups and headers within the menu. It's not highlighting menu items as expected. Here is my XML:
Jammo
  • 1,838
  • 4
  • 25
  • 39
14
votes
4 answers

android getMenuInflater() in a fragment subclass - cannot resolve method

I am trying to inflate a menu in a class that inherits the Fragment class. Here is my OnCreateOptionsMenu() method - @Override public boolean OnCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.forecastfragment, menu) ; return…
Flame of udun
  • 2,136
  • 7
  • 35
  • 79
14
votes
3 answers

searchManager.getSearchableInfo(getComponentName()) returns null

I've followed this Android guide in order to add a search bar to an activity. The setup looks like this: res/menu/activity_main.xml:
Chris
  • 3,192
  • 4
  • 30
  • 43
13
votes
2 answers

Use different icons with different Android SDK versions

I have icons for my Android menu. On Android 3+ I'm using a black ActionBar so the icons are white. However, on Android 2.x the menu is inherently white which means the icons are nearly invisible. How can I use different menu icons for different…
Kenny Wyland
  • 20,844
  • 26
  • 117
  • 229
13
votes
4 answers

Fragments, with the same menu, on the same layout cause duplicated menuitem

I have a layout which has 2 fragments. These fragments uses the same menu, because I have another layouts that has only one of these (used in small screen device). My problem is menu items of each fragments shows in the menu, and since they're from…
mr_tawan
  • 666
  • 1
  • 5
  • 8
13
votes
3 answers

How to open sub menu after click on menu-item in Navigation drawer?

I implemented a navigation drawer, with navigation view. and i am adding value in navigation view through a menu.xml…
Himanshu Shekher Jha
  • 1,334
  • 2
  • 14
  • 27
13
votes
4 answers

OnOptionsItemSelected in activity is called before onOptionsItemSelected in fragment. Other way possible?

I have an activity which can contain several fragments. Each of the fragments can have their own menu entries in the ActionBar. This works fine so far and each item is clickable and performs the desired action. My problem is the following. In the…
MrHill
  • 1,271
  • 3
  • 12
  • 19
12
votes
7 answers

how to center icons in toolbar in android

I asked a similar question here... I got some tutorials in the answers. But this question is diffrenet. because none of that method do not works in my project. I want center all icons in toolbar I test all availabe ways ...but always icons are…
partiz
  • 1,206
  • 2
  • 13
  • 34
12
votes
8 answers

Android Toolbar Popup menu not showing icons

I am trying to show a drop down menu for my toolbar which includes BOTH text and icons:
checklist
  • 12,340
  • 15
  • 58
  • 102
12
votes
7 answers

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

as the title suggest, I'm getting that error in at least 3 of the XML layout files, however, I dont see the attritube "showsAsAction" in any of those XML files, did I miss something or am I just blind?, here are the XML files in question:
Moko
  • 441
  • 4
  • 9
  • 15
12
votes
4 answers

How to implement dynamic values on menu item in Android

I have a menu item in the action bar. Along with the menu item image, I need to show some number associated with it which will change often. I am not using Action bar sherlock. I don't want to use that. Other than this everything else just works…
intrepidkarthi
  • 3,104
  • 8
  • 42
  • 75
11
votes
3 answers

Clear Toolbar menu options added by fragment when it is replaced

I have a set up of 3 'top-level' fragments that each use their own Child Fragment Managers to offer drill-down navigation. These top-level fragments are switched around by being replaced using the main activity's Support Fragment Manager. One of…
11
votes
5 answers

Add one section separator for Navigation Drawer in Android

I have a navigation drawer like this image. I want to add a section separator . It seems simple but I can't find anything on the web that was useful for my case. - Add a line separator below Express Add a line separator below My Information …
Tufan
  • 2,789
  • 4
  • 34
  • 52
11
votes
1 answer

How to access views insde the actionLayout in menu

I have three menu items which includes same app:actionLayout it has a textview, how can i access the textviews individually through code and set different text for all three textviews. activity_main_drawer.xml
Sai
  • 15,188
  • 20
  • 81
  • 121