7

I am developing an Android application using Honeycomb android 3.0 . I am trying to display a menu in Action Bar . The menu has an icon and title. When we click the menu item it displays its items in the form of a dropdown list. It was the items in drop down list with item names but without icon it is displaying.

I want an icon to be displayed beside the title in drop down list which appears when I click the menu. can anyone help me in sorting out this issue. My XML file is as below:

<?xml version="1.0" encoding="utf-8"?>           
<menu  xmlns:android="http://schemas.android.com/apk/res/android">  
<item          
  android:id="@+id/addserver"  
  android:icon="@android:drawable/ic_menu_add"   
  android:title="Add Server"    
  android:showAsAction="ifRoom|withText"     
>  
 
<menu>    
            <item android:id="@+id/fileserver"    
                  android:icon="@android:drawable/ic_menu_add"  
                  android:title="File Server"          
                  android:onClick="onCreate"           
                  android:showAsAction="always"/>      
            <item android:id="@+id/sharepoint"            
                  android:icon="@android:drawable/ic_menu_add"  
                  android:title="Share Point"          
                  android:onClick="onCreate" />          
        </menu>            
</item>

  

Initially it displays add server with icon on left. clicking on that will display fileserver, sharepoint as dropdown list without icon though I given android:icon statement.

How can I sort out this issue?

halfer
  • 19,824
  • 17
  • 99
  • 186
Android_programmer_camera
  • 13,197
  • 21
  • 67
  • 81
  • possible duplicate of [Actionbar not shown with AppCompat](http://stackoverflow.com/questions/18510337/actionbar-not-shown-with-appcompat) – Junior Mayhé Jun 28 '14 at 21:02
  • possible duplicate of [How To show icons in Overflow menu in ActionBar](http://stackoverflow.com/questions/18374183/how-to-show-icons-in-overflow-menu-in-actionbar) – user1931751 Sep 16 '14 at 10:28

3 Answers3

10

The behavior where icons are not displayed in the action bar's overflow menu is by design (as of this writing). If you absolutely need to use icons, you'll need to write a custom implementation consider rethinking your design to fit the UI conventions.

Roman Nurik
  • 29,665
  • 7
  • 84
  • 82
  • Thanks Roman . I will be pleased if u can provide anny sample code or a link for customized overflow menu. – Android_programmer_camera Mar 07 '11 at 07:20
  • 11
    @Android_programmer_camera: Please leave the overflow menu alone. Please do not break UI patterns and conventions. Please do not give users (and the media) all the more reason to complain that Android developers follow no UI conventions and therefore all Android apps look and work differently, making it difficult to use Android. Again, please leave the overflow menu alone. – CommonsWare Mar 07 '11 at 12:29
  • 1
    +1 @commonsware can you please specify the list of UI patterns **Android Application** have to follow. – Ganapathy C May 14 '11 at 04:56
  • 28
    Considering a sub-menu in an actionabar menu uses icons by default, I see this as a completely ridiculous "pattern", and what about internationalisation? It's impossible to translate an app into every language in the world, but I imagine every culture would get the meaning of a speech bubble or envelope menu item – Steven Elliott Jun 24 '12 at 17:28
  • 10
    I definitely agree with Steven, submenus have icons and menus don't? Nice pattern! Even menus and submenus work and look differently in Android, very good. – devmiles.com Aug 24 '12 at 13:58
  • 1
    @Ganapathy http://developer.android.com/design/patterns/actionbar.html and http://developer.android.com/guide/topics/ui/index.html – TWiStErRob Sep 12 '14 at 13:17
  • @devmiles.com http://android-developers.blogspot.hu/2012/01/say-goodbye-to-menu-button.html – TWiStErRob Sep 12 '14 at 13:19
  • This deficiency is still not fixed in 2017. I wonder what are those android guys doing besides smoking pot. – f470071 Mar 21 '17 at 20:54
2

Actually, there is a way to put icons next to the texts for the menu items:

final MenuItem menuItem=...
final ImageSpan imageSpan=new ImageSpan(this,R.drawable.ic_stat_app_icon);
final CharSequence title=" "+menuItem.getTitle();
final SpannableString spannableString=new SpannableString(title);
spannableString.setSpan(imageSpan,0,1,0);
menuItem.setTitle(spannableString);

This will put an icon at the beginning of the menu item, right before its original text.

BTW, this will also work on PopupMenu.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
1

Though the original question is a bit old and moreover, because reasoning against showing icons in the menu is somewhat lacking substance (see Steven Elliott's excellent remark Displaying icon for menu items of Action Bar in Honeycomb android 3.0), I'd like to point to a great, working solution that was given here:

@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) { //...
            } catch(Exception e) { // ...
            }
        }
    }
    return super.onMenuOpened(featureId, menu);
}

Simply add this code to your activity and import the appropriate modules. Again, not my work, but working none the less.

Community
  • 1
  • 1
user1931751
  • 594
  • 3
  • 13