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
19
votes
6 answers

Android: customize application's menu (e.g background color)

What is the way (if there is a way) to customize the menu (the one triggered by the MENU button of the phone). I'm especially interested in two things: changing the background color from the standard light gray into a darker gray how the menu items…
znq
  • 44,613
  • 41
  • 116
  • 144
19
votes
3 answers

How can I focus on a collapsible action view EditText item in the action bar (when it is expanded) and force the soft keyboard to open?

I am using Jake Wharton's excellent ActionBarSherlock library and have a collapsible search action view. I want to popup the soft keyboard when the search action view is expanded. We can read a recommended way of doing this in a DialogFragment in…
17
votes
7 answers

How to make option menu appear on bottom of the screen?

Here is what I am trying to implement: when people click on the menu on top right corner of a toolbar, an options menu appears on bottom of the screen. See picture below: I am not sure what method should I call for the item at the bottom. Can…
ken
  • 2,426
  • 5
  • 43
  • 98
17
votes
5 answers

BottomNavigationView Original icon color

I have my bottomNavigationView : And i added this class to prevent it from doing shiftingMode : public class BottomNavigationViewHelper { public static void disableShiftMode(BottomNavigationView view) { BottomNavigationMenuView menuView…
user7913931
17
votes
1 answer

Bottom navigation view with custom item (actionLayout)

I would like to add a custom item in the new BottomNavigationView . There are plenty of tutorial of adding a custom view with the normal navigation view but I can't find anything regarding the bottom one. This is my view …
Manza
  • 3,427
  • 4
  • 36
  • 57
17
votes
1 answer

app:showAsAction vs android:showAsAction

I am building an Android app and want to have a button always visible on the actionBar. I followed this guide and others, but none of them seem to solve my problem (although they are very close I guess...). I have the package "app" and use…
3yakuya
  • 2,622
  • 4
  • 25
  • 40
17
votes
2 answers

Android align menu items to left in action bar

i have action bar in my application that displays menu items defined in my res/menu/activity_main.xml My menu items are aligned to right on action bar. I want them to be aligned to left. Only solutions i found for this used custom action bar, like…
hendrix
  • 3,364
  • 8
  • 31
  • 46
16
votes
8 answers

openOptionsMenu function not working in ICS?

Im using action bar compability library. Im trying to open the options menu from a button with openOptionsMenu() function but it does nothing. Menu shows as usual when pressing the menu key on my phone. What is wrong here? public class ReadActivity…
Richard
  • 14,427
  • 9
  • 57
  • 85
16
votes
12 answers

Navigation Drawer item remains selected Android

My navigation drawer keeps showing the last selected item.Is there any way to remove it.I want that if the user is at Home page, the navigation drawer items should be non-highlighted. I have tried drawer.setSelected(false); in onResume(). But it…
The Bat
  • 1,085
  • 1
  • 13
  • 31
16
votes
7 answers

How to set a long click listener on a MenuItem (on a NavigationView)?

How can I set a long click listener on a MenuItem? I tried this answer, but the method doesn't exist for me. Any solutions? Code: Menu menu = navigationView.getMenu(); MenuItem menuItem = menu.findItem(R.id.menu_item); // TODO set a long click…
Thomas Vos
  • 12,271
  • 5
  • 33
  • 71
16
votes
4 answers

Reduce Menu items width,height and textview size

I had done a menu overflow items.I need to reduce menu items width,height and textview size. I referred this post.But it is not working for me.I am posted the code and screenshot related to that:
Stephen
  • 9,899
  • 16
  • 90
  • 137
16
votes
3 answers

How can I change option menu in different fragments?

I have a Fragment with menu: public class FragmentA extends Fragment { public FragmentA() { setHasOptionsMenu(true); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); …
alfo888_ibg
  • 1,847
  • 5
  • 25
  • 43
16
votes
4 answers

menu inflating calls multiple times at fragment's onCreateOptionsMenu

I use Fragments and when I switch to nested Fragment, which implements public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) my menu inflates quantity of times when I get to that nested Fragment. How can I avoid this? I also implement…
Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81
15
votes
1 answer

Inheritance and extension of XML (menu) resources

Is it possible to inherit and extend XML resources in android easily, specifically for menus. For example. if my base_menu.xml is
Chris Bye
  • 1,028
  • 7
  • 17
15
votes
5 answers

How to hide menu on fragment (which is being replaced from the layout)?

I have checked and tried out all the possible codes here but those didn't work for me as i am replacing the layout only not having different toolbar.I am using fragments and NavigationDrawer, the navigationdrawer has listview, when a listitem is…
silverFoxA
  • 4,549
  • 7
  • 33
  • 73