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>
Related
We switching back to showing Actionbar
(source: android.com)
But [3] action overflow menu is not shown (in Android 4.3, Samsung Note 2).
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
Activity class extends android.app.Activity (not ActionBarActivity)
I played to even setting all menu item to have android:showAsAction="never" (In that case no actions are on ActionBar)
Docs
http://developer.android.com/guide/topics/resources/menu-resource.html
Similar questions when using appcompat support library
Android ActionBar compat overflow menu not showing on sdk 10
how to show menu item under overflow icon in android api 8+
EDIT common_actions.xml has all actions as android:showAsAction="never"
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_home"
android:icon="#drawable/ic_default_image"
android:showAsAction="never"
android:title="#string/recommend"
/>
<item
android:id="#+id/action_downloadmanager"
android:icon="#drawable/icon_personal_light_download_manage"
android:showAsAction="never"
android:title="#string/downloadmanage"
/>
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:showAsAction="never"
android:title="#string/search"
/>
<item
android:id="#+id/action_settings"
android:icon="#drawable/ic_action_settings"
android:showAsAction="never"
android:title="#string/setting"
/>
</menu>
This is a Samsung thing that's actually a reaaaaallly stupid UI decision in my opinion. Within Touchwiz for all versions AFAIK overflow in the ActionBar isn't supported, instead you have to hit the bottom left button where a Gingerbread-esque menu pops up.
With Toolbar in AppCompat though you can override that behaviour and it shows a normal overflow in your ActionBar, I wrote a short post about moving from ActionBar to Toolbar that you can check out here, which contains links to more resources.
Looking further I have found a similar question https://stackoverflow.com/questions/23906985/action-overflow-button-not-shown (and here too)
that actually lead to several options inside Android action bar not showing overflow
I went with
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
makeOverflowMenu();
}
private void makeOverflowMenu() {
//devices with hardware menu button don't show action overflow menu
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) {
Log.d(TAG, e.getLocalizedMessage());
}
}
Hardware menu button on Samsung Note 2 acts as if ActionBar action overflow menu was pressed.
That is correct behavior.
If you have a physical menu button (Samsung has it), Android will automatically hide the three dots.
If you don't, it will show them.
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.
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>
I'm having a problem when I try to set one item in my actionbar as always visible and 4 more icons as dropdown items with the following layout:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/search_movies"
android:icon="#drawable/action_search"
android:showAsAction="always"
android:title="Search"/>
<item
android:id="#+id/movies"
android:icon="#drawable/action_video"
android:showAsAction="collapseActionView"
android:title="Movies"/>
<item
android:id="#+id/theaters"
android:icon="#drawable/action_location_map"
android:showAsAction="collapseActionView"
android:title="Theaters"/>
<item
android:id="#+id/preferences"
android:icon="#drawable/action_settings"
android:showAsAction="collapseActionView"
android:title="Preferences"/>
<item
android:id="#+id/contact"
android:icon="#drawable/action_about"
android:showAsAction="collapseActionView"
android:title="Contact"/>
</menu>
The result is just the first item showing and the rest are not visible, not even as a dropdown. This is using ActionBarSherlock and a 2.3 Android device.
The question is, how can I get the icons to follow this layout:
EDIT:
The problem I had was because when you are using the actionbar with a device that has a "menu" hardware button the 3-dot dropdown does not shows off, the 4 other items are only displayed if you press the menu hardware button. Does anyone knows if this behaviour can be modified?
Hmmm, maybe I misunderstood, but if you wish to places those remaining four items into the overflow action menu (the 3-dot icon) then using android:showAsAction="never" instead of "collapseActionView" should do it.
...Tried a couple ways, but this did the trick:
Force overflow menu in ABS
I've met the same problem and my solution is quite simple. (I didn't use HoloEverywhere.)
The idea comes from the ABS sample project, whose drop-down menu can be displayed on pre-4.0 devices as well by using a submenu. So, my idea is using a submenu to disguise the 3-dot icon. Here's the code:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
SubMenu sub = menu.addSubMenu("More");
sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
sub.getItem().setIcon(R.drawable.ic_menu);
getSupportMenuInflater().inflate(R.menu.activity_main, sub);
return true;
}
Since the "More" menu doesn't have a MenuItem.SHOW_AS_ACTION_WITH_TEXT attribute, so the word "More"(or whatever you named) will actually not be displayed on the action bar. The only displayed icon R.drawable.ic_menu can be copied from ABS source code res/drawable-xxdpi folders named "abs__ic_menu_moreoverflow_normal_holo_dark.png", which is the so-called 3-dot icon. And the R.menu.activity_main is your menu xml.
It works!
I'm writing an android application (sdk 8 to 15) and I'm dealing with the menu. I read a lot of things on it to understand, but I 'm stuck on one point :
Apparently since sdk 10, hardware menu button isn't supported, now we have to use the action bar. Ok. But in my app I want to have this kind of menu :
Or this :
... which are not hardware menu button but seems to be 'native action bar'. All I can find on the web is an action bar on the top, with the title of the app and a logo on the left.. which take a lot of place.
I'm so lost with all those sdk versions and I just want to integrate a simple menu like above, how should I proceed ?
I have this in my code, but :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_fragments_slider, menu);
return true;
}
... but no menu appears with android 4 as it should be in the first or second image.
Check out the Menu Resource in the Android API Documentation and especially:
android:showAsAction=["ifRoom" | "never" | "withText" | "always" |
"collapseActionView"]
The example XML from the documentation is a good resource also for exactally how to lay it out:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/item1"
android:title="#string/item1"
android:icon="#drawable/group_item1_icon"
android:showAsAction="ifRoom|withText"/>
<group android:id="#+id/group">
<item android:id="#+id/group_item1"
android:onClick="onGroupItemClick"
android:title="#string/group_item1"
android:icon="#drawable/group_item1_icon" />
<item android:id="#+id/group_item2"
android:onClick="onGroupItemClick"
android:title="#string/group_item2"
android:icon="#drawable/group_item2_icon" />
</group>
<item android:id="#+id/submenu"
android:title="#string/submenu_title"
android:showAsAction="ifRoom|withText" >
<menu>
<item android:id="#+id/submenu_item1"
android:title="#string/submenu_item1" />
</menu>
</item> </menu>
You then need to add to your Manifest:
android:uiOptions="splitActionBarWhenNarrow"
That will move your buttons down to the bottom, leave the title in the actionbar and if you have more than what can fit in the screen you get the little three dot | on the right of the lower actionbar. This only really works in Portrait mode though, unless you have quite a few menu items... or longer text strings (I think...)
ActionBarSherlock puts your menu stuff in the ActionBar across all supported platform versions.
Add it as library project and make sure that you import the Sherlock Menu and extend SherlockActivity.