How to change Option Menu Item layout? - android

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.

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.

how to add icon in menu items in action bar 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.

Action bar (Sherlock) menu items cutting off built-in Spinner

My action bar menu items are cutting off the width of the action bar Spinner so that the Spinner's text gets truncated. I've set all my menu items to use "ifRoom" but unfortunately they interpret the original Spinner width as available room.
Here's the truncation when I have three menu items (yes, I know the third icon looks identical to the second):
Here's what I get when I comment out the third menu item. Ultimately, I want something that looks like this, but with an overflow icon showing instead of the search icon:
I populate the Spinner using a custom adapter (extending from BaseAdapter and implementing SpinnerAdapter).
Here's my menu XML:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/ab_refresh"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="#string/ab_refresh"
android:icon="#drawable/navigation_refresh"/>
<item
android:id="#+id/ab_search"
android:orderInCategory="2"
android:showAsAction="ifRoom|collapseActionView"
android:title="#string/ab_search"
android:icon="#drawable/action_search"
android:actionViewClass="com.actionbarsherlock.widget.SearchView"/>
<item
android:id="#+id/ab_toggle"
android:orderInCategory="3"
android:showAsAction="ifRoom"
android:title="#string/ab_latest"
android:icon="#drawable/action_search"/>
</menu>
And here's my ab_dropdown XML (used in getView of my custom adapter):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="fill_horizontal">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="5dp"
android:layout_marginTop="1dp" />
</RelativeLayout>
What I've tried:
Setting a minWidth of 570dp for the RelativeLayout and TextView of ab_dropdown. No effect regardless of whether I set it in XML or the getView method of my custom adapter.
Hardcoding the layout_width of RelativeLayout and TextView to 570dp. Again, no effect in XML or code.
I WOULD attempt to find the action bar Spinner programmatically so as to set its width, but when I tried to do this for a different reason, I couldn't.
Any ideas?
Personally, I'd switch to a navigation drawer and dump the drop-down list navigation.
That being said, it sounds like you need two versions of your menu resource. Have the third item be never in the toolbar (always in overflow) by default, and have it be ifRoom on larger screens (e.g., res/menu-sw600dp/).
You could also experiment with AutoScaleTextView for your drop-down list navigation labels.
The navigation portion of the action bar is deemed least important, which is why ifRoom considers there to be room.

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!

Add dropdown on custom action bar view at left, with checkable items

I have a lot of problems with the development of an action bar. Here are my needs:
I want to add a dropdown in my action bar, which is using custom view because of some custom comportment (animation for example).
The dropdown have to be accessible from an image button (not a spinner which show a text area of the selected element).
The image button that allow to show the dropdown have to be at the left of the action bar.
The dropdown that appear have to contain checkable items, and, when the user check an item, the dropdown have to not be hidden (this allow the user to check multiple items at once).
My application have to work from android 2.3 (I'm using action bar sherlock for that, I specify this because of I can't use PopupMenu on android 2.3).
I have successfully added a dropdown in my action bar by combining my custom view (actionBar.setCustomView(...)) with a menu (menuInflater.inflate(...)). I think the code of my customview is not very important, here is the code of my menu :
<item
android:title="test"
android:drawable="#drawable/dropdown_icon"
android:showAsAction="always">
<menu>
<item
android:id="#+id/item1"
android:title="item 1"
android:checkable="true"/>
<item
android:id="#+id/item2"
android:title="item 2"
android:checkable="true"/>
<item
android:id="#+id/item3"
android:title="item 3"
android:checkable="true"/>
</menu>
</item>
With this solution, I have the following problems :
I'm not able to put the dropdown item at left (it's at the right for the moment)
When I check an item on the dropdown list, my dropdown hide itself. I want that the dropdown keep its showing state.
How can I achieve what I want ?
P.S : As you can read, English is not my native language. These problems are difficult to explain (even in my native language), so if some sentences are not understandable, please tell me and I will try to improve them.
I suggest doing the simplest thing possible: remove all the ActionBarSherlock related code and add your own views in layout and program them to animate, pop-up, show and hide.

Categories

Resources