How to Force Use of Overflow Menu on Devices With Menu Button

How to force use of overflow menu on devices with menu button

EDIT: Modified to answer for the situation of physical menu button.

This is actually prevented by design. According to the Compatibility Section of the Android Design Guide,

"...the action overflow is available from the menu hardware key. The resulting actions popup... is displayed at the bottom of the screen."

You'll note in the screenshots, phones with a physical menu button don't have an overflow menu in the ActionBar. This avoids ambiguity for the user, essentially having two buttons available to open the exact same menu.

To address the issue of consistency across devices: Ultimately it's more important to the user experience that your app behave consistently with every other app on the same device, than that it behave consistently with itself across all devices.

Is it possible to force use of overflow menu to the right of software button?

Is it possible

Yes, if you do not mind your app looking like you are no longer maintaining it, thereby reducing your prospective user base.

Please read Say Goodbye to the Menu Button, particularly this passage:

However, this button doesn’t provide an ideal user experience. In fact, in apps that don’t use an options menu anyway, this action overflow button does nothing and creates user confusion. So you should update your legacy apps to remove the action overflow from the navigation bar when running on Android 3.0+ and begin using the action bar if necessary. You can do so all while remaining backward compatible with the devices your apps currently support.

Talented developers, therefore, are trying to avoid putting that affordance in the system/navigation bar, not adding that affordance.

how to do this ?

Set your android:targetSdkVersion and your android:minSdkVersion to something less than 11.

how to force overflow menu in Actionbar using Actionbarsherlock on 4.0 devices

NOTE: With this suggestion, I am not recommending using ForceOverFlow to any reader. This is simply listing a possibility of making it work (forcing it to work rather). To each his own. Some may want it and like it too. Others may not.

I am probably speculating, but perhaps, this may do it for you.

You can think of this a hack, but I used it before to force the Overflow menu in one of my apps earlier and it works.

try {
ViewConfiguration config = ViewConfiguration.get(MainPage.this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}

Also, the second link by MisterSmith has a solution of sorts with my answer in it. Commonsware has put down some thought about forcing the OverFlow menu here: How To Control use of OverFlow Menu in ICS

EDIT: While typing out this suggestion, you added a comment. To respond to that, I would like to point out that Jake Wharton took out .ForceOverFlow themes. I haven't tried it with version 4.2.0, but with a custom theme, it just might work. If you absolutely must use ForceOverFlow, you might have to use an older version. Read my answer here: https://stackoverflow.com/a/13180285/450534. Something might just make it work.

EDIT 2: As pointed out by the OP in a comment, the Demos Sample APK, in fact, does ForceOverFlow the action bar in Action Modes. I have a feeling, after checking the relevant Java files on github, that the answer to that lies perhaps in 3 Java files.

  1. The Activity ActionModes adds menu items in a very unconventional manner: https://github.com/JakeWharton/ActionBarSherlock/blob/master/samples/demos/src/com/actionbarsherlock/sample/demos/ActionModes.java (Line 53)
  2. The ActionMode Java file in the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/ActionMode.java
  3. The MenuItem Java file again part of the ABS Library: https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/view/MenuItem.java

How to force overflow menu on android actionbar compat?

The action overflow menu is only available when there is no hard menu button available on the device. I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here.

There is an action bar library written by Jake Wharton called ActionBarSherlock. Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this.

Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. I would suggest just looking into using his library all together, as it is very well done.

If you choose to use Jake's library, look into setting up the Activity theme as @style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices.

Edit2: Using ForceOverflow theme causes issues (example #1) on devices with hardware menu button. Thus, Jake Wharton is going to remove ForceOverflow in the future versions.

Forcing Overflow menu button on Android

To force into the overflow menu do this:

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, 2, 0, "Item 1").setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

return true;
}

SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW will force the item into either:

Android 3.0 + : The ActionBar as an overflow
Android 2.3 - : The menu button

If you are wanting to recreate the menu you will need to call invalidateOptionsMenu(); This will re-create from OnCreateOptionsMenu.

How To Control use of OverFlow Menu in ICS

where I am wrong

You are wrong for having this code in your app.

how I can use it

You should not use it. Please allow the overflow menu to behave normally.

First, your code may not work on all devices and Android versions.

More importantly, what you are doing is telling your users that you think that you are more important than they are.

Each device, based on OS version and hardware, has its means of triggering the menu: either the ... affordance in the action bar or the device's MENU button. Users want consistency between apps on their device. For users whose device has a MENU button, they will expect to use the MENU button in all apps to bring up menus where available. You are attempting to harm those users, by breaking their MENU button. Please do not do this. Please allow your app to behave the same way as the rest of the apps on those users' devices, where the MENU button brings up the overflow menu.


UPDATE

As of Android 4.4, the overflow ... affordance will appear in the action bar regardless of whether the device has a MENU button, and Google is putting a bit more pressure on device manufacturers to stop including a MENU button. I have blogged a bit more about this.

Force overflow menu in ActionBarSherlock

If you are using Version 4.2.0, then .ForceOverflow themes have in fact been removed.

Source: Version 4.2.0 Changelog

Extract of the Change Log:

Add SearchView widget for standard search interaction (API 8+ only)
Fix: ShareActionProvider in the split action bar no longer fills the entire screen.
Fix: ShareActionProvider now does file I/O on a background thread.
Fix: Automatically correct ColorDrawable not respecting bounds when used as a stacked background.
Fix: Ensure fragments collection is present before dispatching events.
Fix: XML-defined onClick searches the correct context for the declared method.
Fix: Ensure action mode start/finish callbacks are invoked on the activity for the native action bar.
Fix: Allow tab callbacks to have a fragment transaction instance for any FragmentActivity.
Fix: Ensure CollapsibleActionView callbacks are dispatched in both native and compatbility action bars.
Fix: Remove .ForceOverflow themes. These never should have been included.

If you absolutely need to force Overflow, you will need to download an earlier version of ABS. You can get a list of download as per their release history here: http://actionbarsherlock.com/download.html

I personally still use the ABS version 4.1.0 since I do not currently want to make ancillary changes in my app. I also use this in my theme.xml:

<style name="MyTheme" parent="@style/Theme.Sherlock.ForceOverflow">
<item name="android:windowBackground">@color/background</item>
<item name="actionBarStyle">@style/Widget.Styled.ActionBar</item>
<item name="android:actionBarStyle">@style/Widget.Styled.ActionBar</item>
</style>

<style name="MyTheme.ForceOverflow">
<item name="absForceOverflow">true</item>
</style>

And while applying a theme for an Activity in the manifest.xml, I use this as the attribute: "@style/Theme.SociallyYOU"

Again, if you must absolutely force overflow, you might also want to read CommonsWare's thought on the same in another question here: https://stackoverflow.com/a/12872537/450534.

NOTE: That being said, it is always better to use the latest version if the trade offs aren't to critical. By posting how I force the overflow menu, I am neither suggesting that you use an older version nor do I recommend that. It is merely informing you of the possibilities.



Related Topics



Leave a reply



Submit