How to force action bar overflow icon to show
Congratulations! You won!
As of Android 4.4, the ... affordance in the action bar will be there, regardless of whether the device has a physical MENU button or not. Google's current Compatibility Definition Document now comes out a bit more forcefully against having a dedicated MENU button.
The hack that developers have used in the past, to get this behavior, is:
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
}
catch (Exception e) {
// presumably, not relevant
}
That should not be needed on Android 4.4+, though with the exception handler in place, I would not expect any particular problem if you run it and, someday, they get rid of sHasPermanentMenuKey
outright.
Personally, I still wouldn't change things on Android 4.3 and below, as I suspect it's a whack-a-mole situation, where you will replace complaints about having no menu with complaints about having duplicate versions of the same menu. That being said, since this is now the officially endorsed behavior going forward, I have no problems with developers aiming for consistency on older devices.
A hat tip to the commenter on the issue I filed regarding this, pointing out the change.
How do I show Overflow Menu Items to Action Bar in Android
To show three dot icon, to Action Bar, just use below method in your OnCreate():
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Action bar overflow menu items doesn't appear
Overflow menu is hidden in ActionBar if your device has hardware 'Menu' button.
Press it and check - your items should be there :)
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.
Action bar overflow not displayed
Do I have to force the overflow icon, and if so how can I do so?
All you need to do is have your theme inherit from one of the .ForceOverflow
themes. Note this will only work for Android 2.0 and 3.0. In 4.0 the native ActionBar
component is used instead, and the overflow item is only displayed if the device does not have a menu key.
Related Topics
Download Files and Store Them Locally with Phonegap/Jquery Mobile Android and iOS Apps
Opening Android Settings Programmatically
Why Is My Smallicon for Notifications Always Greyed Out
Programmatically Relaunch/Recreate an Activity
How to Scale Bitmap to Screen Size
Neither User 10102 Nor Current Process Has Android.Permission.Read_Phone_State
How to Make Grid-View Horizontally Scrollable in Android
How to Turn Speaker On/Off Programmatically in Android 4.0
Adding Google Play Services Version to Your App's Manifest
Move Markers in Google Map V2 Android
How to Check If an Intent Can Be Handled from Some Activity
Appcompatv7 - V21 Navigation Drawer Not Showing Hamburger Icon
Start Application Knowing Package Name
Find Out If Listview Is Scrolled to the Bottom
Webview Link Click Open Default Browser