views:

791

answers:

1

Hi,

Can we set Title for a Menu Item in Android if it has an icon from drawable?

I have a icon for a Menu Item and if I set the icon then the title that is set for that Menu item is not visible.

Is this possible or not?

Hope to get a reply soon.

Regards Sunil

+2  A: 

It's definitely possible. Code like this works:

public boolean onCreateOptionsMenu(Menu menu){
    menu.add(0, MENU_PAUSE, 0, "Pause").setIcon(R.drawable.pause);
    return true;
}

That creates a menu with one item in it, referred to by the int MENU_PAUSE (a unique identifier number), with the title "Pause" and the icon pause from res/drawable.

If that doesn't do it for you, could you show us your code?


Edit to provide more info based on comments:

It turns out the size of the images matters. The default Android menu images are 48*48 or smaller so it would seem that one must use similar sizes as well. Note: I didn't experiment with different screen sizes, this was only with 320x480 screen.

The alternative is to change the default background of the menu. These can be modified by setting a theme for your application in the manifest and then putting in these lines:

<item name="android:panelBackground">@drawable/yourMenuDrawable</item>
<item name="android:panelFullBackground">@drawable/yourOtherMenuDrawable</item>

The normal menu uses panelFullBackground, so that should be the one you replace first. You can find the default images Android uses in yourSDKfolder/platforms/xxx/data/res/, and they're called menu_background.9.png and menu_background_fill_parent_width.9.png.

I found this information by looking at the default styles and themes files available from the Android source code website

Steve H
what is the size of the image that you are using? I have four menu items so it takes 2 * 2 and half of the screen for each icon. I am doing the same thing. The text is displayed if I dont add an icon. The icon in my case is of 160 * 68. So it occupies all the height and width of the menu item and so the text is not displayed. Can we display the text on top of icon? Please help.
sunil
Aha, you're right. The size of the image used matters. I hadn't noticed because I'm using the default Android images - they are all 48x48 or smaller, so it would seem you will have to resize your images.
Steve H
Oh ok. Can we change the color of the Menu item from default white to something else, then place the icon of 48 * 48 on it and text below it? Can we do this? Let me know if you have any idea regarding this. Thanks for replying.
sunil
I edited my answer to give more details.
Steve H
Thanks a lot Steve. It has solved my problem. One more thing, can we also change the number of menu items in one row. Currently it has 2 * 2 structure since I have 4 menu items. But what if I wanted to have them all in one row. Is that possible? Anyways, thanks a lot again.
sunil
I don't think you could do that without customising the menu code; by default it adjusts the the position/layout of the buttons depending on how many you have.
Steve H