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
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…
54
votes
7 answers

How to display and set click event on Back Arrow on Toolbar?

How can I set back arrow in Android toolbar and also apply click listener?
54
votes
1 answer

What is the difference between onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu)?

Explain difference between onCreateOptionsMenu(Menu menu) and onPrepareOptionsMenu(Menu menu).
Jumpo
  • 641
  • 2
  • 6
  • 7
52
votes
11 answers

Action Bar menu item text color

How to change text color of menu item title. I tried to change it as below