I would like to force the overflow icon to always show in the action bar (assuming there are overflow items). On models with a menu button, sometimes the overflow icon doesn't appear and users must tap the devices menu button to get the rest of the action menu items. Users keep complaining about this.
Note that for the context menu, the overflow icon always shows, regardless of whether the device has a built in menu button or not.
I realize that forcing the overflow icon to appear in the action bar would duplicate the functionality of the "physical" one. You might consider that violating Androids design guidelines. In my opinion, though, the users win. They say it's confusing and I believe they're right.
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.
This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/saveDetails"
app:showAsAction="always"
android:title="#string/save"/>
<item
android:id="#+id/menu_overflow"
android:icon="#drawable/dts"
android:orderInCategory="11111"
app:showAsAction="always">
<menu>
<item
android:id="#+id/contacts"
app:showAsAction="never"
android:title="Contacts"/>
<item
android:id="#+id/service_Tasks"
app:showAsAction="never"
android:title="Service Tasks"/>
<item
android:id="#+id/charge_summary"
app:showAsAction="never"
android:title="Charge Summary"/>
<item
android:id="#+id/capture_signature"
app:showAsAction="never"
android:title="Capture Signature"/>
</menu>
</item>
</menu>
Related
I implemented the overflow button myself using the following base XML:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="Settings"
android:icon="#drawable/ic_action_overflow"
android:showAsAction="always">
<menu>
<item
android:id="#+id/action_logout"
android:title="Log out"
android:showAsAction="never" />
<item
android:id="#+id/action_offer_advice"
android:title="Advice"
android:showAsAction="never" />
</menu>
</item>
<item
android:id="#+id/action_create"
android:title="Create"
android:icon="#drawable/ic_action_new"
android:showAsAction="always"/>
</menu>
I did this because I want the Overflow button to be on every Android device regardless of a physical menu button. I opted for this over the hacky solution offered many times across Stackoverflow. That being said, I'm a bit fuzzy with ActionBars and I want to be positive that Android's default overflow won't show up for users that don't have a physical menu button. It would be very awkward to have my implementation of the overflow button with Android's default one right next to it.
Am I worrying over nothing? Is this a legitimate concern? If it is a legitimate concern, what could I do to handle that?
Personally I would say that this is not a concern. There are many apps native to the Android operating system (ex. the gallery app) that show the overflow menu despite the device having a physical menu button.
Another situation you may not have considered is the possibility that the user's menu button is not functional on the device, causing your action bar tidying to become a problem.
There is undoubtably a way to achieve this, but what you're trying to achieve may cause more problems than solve.
I have an Android app on the play store (Raleigh Nights) and it has just been brought to my attention that the drawable icons in my overflow button are not showing, although the text is showing properly. Everything is showing properly on 4.3 and I have no idea what may cause the difference. When I debug using an emulator it seems to set MenuItem icon and doesn't throw any exceptions. I've spent hours trying to figure out what is going on to no avail. Again, it works in older versions, but does not show the icon in 4.4.2 (KitKat). It also crashes on some 4.4.2 phones although I can't get it to crash on the emulator.
I have the target set to 19.
minVersion = 11;
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
I'm also going to include the menu button that I have to see if that helps.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Will always be in the overflow -->
<item android:id="#+id/menu_drink_specials"
android:title="#string/drinks"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/menu_food_specials"
android:title="#string/food"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/menu_events"
android:title="#string/events"
android:icon="#drawable/added"
android:showAsAction="never"/>
<item android:id="#+id/sort_location"
android:title="#string/sortLocation"
android:icon="#drawable/added"
android:showAsAction="never"/>
</menu>
Has anyone else run across this problem? It seems odd that it works so well in the the other versions and isn't throwing any errors up.
Thanks for your time,
Mike
Are you certain that you have icons showing next to items in the overflow menu? This is intentionally not allowed:
Displaying icon for menu items of Action Bar in Honeycomb android 3.0
It seems like there are several hacks to make something like this work. Maybe you're using one of them and it's what's causing your crash.
However if you want full control over this, it may be best to extend PopupWindow and simply inflate whatever layout you'd like into it. You could then create a 'fake' overflow button in the action bar and configure the PopupWindow to display beneath it.
The way you have your items set up with android:showAsAction="never" will never put icons into the overflow menu. Android, by default, does not allow this. The only way to display icons is to make showAsAction equal to always or ifroom and also have android:icon set. Your app will most likely look fine without the icons in the overflow menu.
Try to put this code on your activity.
#Override
public boolean onMenuOpened(int featureId, Menu menu)
{
if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
if(menu.getClass().getSimpleName().equals("MenuBuilder")){
try{
Method m = menu.getClass().getDeclaredMethod(
"setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
}
catch(NoSuchMethodException e){
Log.e(TAG, "onMenuOpened", e);
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
return super.onMenuOpened(featureId, menu);
}
add these attributes to the menu tag and try
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
You configured "android:showAsAction="never"" and this way and will never display the icons into the overflow menu.
Try change your code like this
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Will always be in the overflow -->
<item android:id="#+id/menu_drink_specials"
android:title="#string/drinks"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_food_specials"
android:title="#string/food"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/menu_events"
android:title="#string/events"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
<item android:id="#+id/sort_location"
android:title="#string/sortLocation"
android:icon="#drawable/added"
android:showAsAction="ifRoom"/>
</menu>
I have tried setting the:
android:showAsAction=".."
to every one of these:
ifRoom, never, withText, always, collapseActionView
but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button.
Here is a picture of the menu now :
<item android:id="#+id/smth1"
android:title="#string/smth1"
android:showAsAction="always"
android:orderInCategory="1" />
I have even tried adding this:
android:uiOptions="splitActionBarWhenNarrow"
into application manifest file, but with no positive result (nothing changed).
I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result.
If my question seems to be unclear, here is a picture of a menu, which I would like to have:
Thanks for any help.
You need to use the compatibility namespace (see here)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always"/>
</menu>
Once you're using that you can use as many menu buttons as will fit. You're no longer limited to just two buttons + overflow showing.
You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar.
How do you manage Icons with ABS and Gingerbread? As you can see from the Screenshots, I have a menu item "Help", that sits in the action bar, when there's room for it. (when nothing is selected). The icon looks good so far. But when it moves into the options menu, I probably should use another icon for better visibility :)
How do you normally do that? Any ideas? This is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/new_button"
android:showAsAction="always"
android:title="new"
android:icon="#drawable/content_new_calendar"/>
<item
android:id="#+id/share_button"
android:visible="false"
android:showAsAction="ifRoom"
android:title="share"
android:icon="#drawable/social_share"/>
<item
android:id="#+id/help_button"
android:showAsAction="ifRoom"
android:title="help"
android:icon="#drawable/action_help"/>
</menu>
I know how to create different menus for various api levels, etc. But here, the same phone is showing the icon in the action bar and in the options menu. The simplest solution is to set android:showAsAction="never", but from user reviews I learned, that the App is difficult to understand (especially the Widget-Part), so I would love to have the help menu visible. Any ideas?
P.S. I know that the App is ugly and unfinished. It's work in progress and will be polished :)
Try using the force overflow option like so:
<item name="absForceOverflow">true</item>
In the Gmail app, the search bar moves all the way to the left when you click on the search icon. Does anyone know how to recreate this effect? The current way I have it, the icon expands when clicked, but doesn't move to the left like the Gmail app. Here is my menu xml file:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_search"
android:title="Search"
android:icon="#drawable/ic_menu_search"
android:showAsAction="ifRoom"
android:actionViewClass="android.widget.SearchView"
/>
</menu>
Take a look at this SO Question and see if that is what you're referring to:
Contextual Action Bar in Honeycomb
and
Contextual Action Bar in Honeycomb
The Gmail app seems to simply have a search icon in the menu and when you click on this it will add SearchView as the current navigation view with getActionBar.setCustomView().
There are a few gotchas when doing this, for example handling when it should be removed etc but if handled well it can lead to a nice user experience. But it may not be worth the hassle though, the regular expandable SearchView should be sufficient for most applications.