Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying icon for menu items of Action Bar in Honeycomb android 3.0

hi
I am developing an android application using Honeycomb android 3.0 . I am tryig to display a menu in Action Bar . The menu has an icon and tiltle. 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 with out 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 with out icon though I given android:icon statement.
Can anyone help me in sorting out this issue?
Thanks in Advance,

like image 678
Android_programmer_camera Avatar asked Mar 07 '11 06:03

Android_programmer_camera


People also ask

What is the Action bar in android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button. In general an ActionBar consists of the following four components: App Icon: App branding logo or icon will be displayed here.

Where is menu in android studio?

To define the menu, create an XML file inside your project's res/menu/ directory and build the menu with the following elements: <menu> Defines a Menu , which is a container for menu items.


2 Answers

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.

like image 153
Roman Nurik Avatar answered Nov 11 '22 11:11

Roman Nurik


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.

like image 37
android developer Avatar answered Nov 11 '22 11:11

android developer