how to add icon in menu items in action bar android? - android

My problem is how it is add the icon in menu items in ActionBar android.if we declare the app:showAsAction="never"
My code is
<item android:id="#+id/twitID"
android:title="twitter"
android:orderInCategory="100"
android:icon="#drawable/twitter"
app:showAsAction="never" />
it show only title.
it's not working.please help me.
Thank u.

You should change showAsAction to showAsAction="always" or showAsAction="ifRoom" and it will automatically put your item to the ActionBar as an item with icon.
If you want to show the item in the overflow menu not in the ActionBar as an item, then you should inflate a PopUp manually with custom layout. Then you can put whatever you want. But it is not simplest thing to do.

Related

android toolbar menu item button?

is it possible to add a button label like this as a toolbar action menu item?
I tried the following code below
<item
android:id="#+id/action_submit"
android:title="#string/submit_titlecase"
android:visible="false"
android:icon="#drawable/primary_btn"
app:showAsAction="always|withText" />
And it appears blank?
If i remove the android:icon, it appears the text but not the button sourrounding outline
Define a menu item in the following manner:
<item android:id="#+id/action_submit"
android:title="Submit"
app:showAsAction="always"/>
(Basically, if you show as action and you don't provide an icon, the title is shown as a button on the toolbar.)
But if you want it specifically to look like a button like you've put up in your question, make a drawable/mipmap file looking exactly like that and then add android:icon="#drawable/icon" (or #mipmap/icon) into your menu item code.

Items are directly added in overflow menu in Actionbar

I'm developing an application in which i need to show a button with an icon on action bar. I tried following but it directly adds in overflow menu. I want it on action bar.
I tried a few solution given here on stackoverflow but none of them worked for me. please tell me what i'm missing
here is xml file of menu.
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/action_wishlist"
android:icon="#drawable/wish"
android:title="WishList"
android:orderInCategory="0"
app:showAsAction="ifRoom" />
</menu>
it Adds item named 'Wishlist' in overflow menu.
Theme i'm using is
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">
thanks.
If you are using Theme.Holo.Light, that means that you are not using appcompat-v7 for the action bar, but instead you are using the native action bar. In that case, change app:showAsAction to android:showAsAction.

How to change Option Menu Item layout?

How to customize the Option Menu item layout from xml or programmatically.
I found many question but no good answer yet.
How to change Option Menu Style
How to change layout by clicking items of menu?
How to change Menu Item Color & Size programmatically?
<item
android:id="#+id/itemId"
android:title="Item title"
app:showAsAction="always"
app:actionLayout="#layout/menu_item_layout" />
Note that this layout will be shown only in the ActionBar. If the item is in the overflow menu only title will be shown.

ActionBarSherlock collapseActionView

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!

Make menu items fill height of action bar

I have an app with an action bar. I need the menu items to fill the height of the action bar. I have tried styling the action bar with android:padding="0dp" and settings android:layout_height="fill_parent" on the action views to no avail.
This picture shows that the browse subjects actionview for instance does not fill the actionbar.
Here is a shortened version of my menu declaration.
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_more"
android:title="More"
android:showAsAction="always"
android:actionProviderClass="pacakge.action_providers.MoreProvider"
/>
</menu>
The action provider class inflates and returns a view that is inserted into the ActionBar.
Any help would be greatly appreciated! Thanks
Ended up just hard coding the button height to 50dp. Not a very elegant solution but it works.
Hmm....looks like you need to use bigger icons.
Also, in the layout for the buttons, use android:layout_height as "fill_parent".
And as a last tip, use android:showAsAction="ifRoom"
Try to change menu item layout to RelavtiveLayout

Categories

Resources