Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Handle Both Action Bar and Menu Button?

Tags:

android

With the game I'm developing, I have an in-game menu the pops up when the menu button is pressed. However, I recently found out that the newer versions of Android don't have a Menu button, but instead an Action Bar.

To note, I'm not using an actual Android menu. All I'm doing is detecting that the Menu button was pressed, and then handling the event from within the game. Which means all I need is something that the user can press, and detect that it was pressed.

So my question is, how can I support both a menu button and the newer action bar? I would like my app to support API levels 7-current.

UPDATE

Okay, after reading that article, I'd like to rephrase my question. It said that it "adds the action overflow button beside the system navigation." My manifest has android:minSdkVersion="7" android:targetSdkVersion="13" but no overflow button appears on my emulator. The emulator is API 14 with skin WSVGA

like image 455
Jesse Jashinsky Avatar asked Mar 06 '12 03:03

Jesse Jashinsky


People also ask

What is the difference between action bar and toolbar?

The Toolbar is basically the advanced successor of the ActionBar. It is much more flexible and customizable in terms of appearance and functionality. Unlike ActionBar, its position is not hardcoded i.e., not at the top of an activity.

How do I replace my action bar toolbar?

To replace an app's default action bar with a Toolbar : Create a new custom theme and modify the app's properties so that it uses this new theme. Disable the windowActionBar attribute in the custom theme and enable the windowNoTitle attribute. Define a layout for the Toolbar .

What does the onOptionsItemSelected () method receives as a parameter?

If an action is selected, the onOptionsItemSelected() method in the corresponding activity is called. It receives the selected action as parameter.


2 Answers

This post in the Android Developers blog has a good discussion of the design transition from the old Menu Button to use of the new Action Bar and Action Overflow list.

It also includes some specific suggestions as to how to deal with your code when transitioning from pre-level 11 to the new Action Bar. May be helpful to you but for your specific application you will need to decide if you want to display an action bar or not. If not then you may want to add a button to your game interface that duplicates the functionality of the menu button on devices that have one.

like image 124
PaulP Avatar answered Sep 22 '22 17:09

PaulP


Newer versions of Android do have a Menu button (see quote below). I think you best option is to just hide the Action bar, and have your game use the menu button in the same way as on older Android versions.

Quoting from Android 4.0 Compatibility Definition

7.2.3. Navigation keys The Home, Menu and Back functions are essential to the Android navigation paradigm.

Device implementations MUST make these functions available to the user at all times when running applications.

These functions MAY be implemented via dedicated physical buttons (such as mechanical or capacitive touch buttons), or MAY be implemented using dedicated software keys, gestures, touch panel, etc.Android 4.0 supports both implementations.

And:

Device implementation MUST present a Menu key to applications when targetSdkVersion <= 10 and SHOULD NOT present a Menu key when the targetSdkVersion > 10.

like image 20
Frank Harper Avatar answered Sep 24 '22 17:09

Frank Harper