setShowsAsAction not working below API 11 - android

This is the code I used to remove a MenuItem:
af.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
However, setShowsAsAction requires minimum API 11 so it crashes on GingerBread. I can use af.setVisible(false); to get the code to work on GingerBread. What are the differences between the two? Is it the same thing?

Use the v7 support library as followed:
MenuItem menuItem = menu.add(....);
MenuItemCompat.setShowAsAction(menuItem , MenuItemCompat.SHOW_AS_ACTION_NEVER);

1. af.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
This will cause the MenuItem to be hidden fro the ActionBar. However it will be visible in the OverFlow Menu.
2. af.setVisible(false);
This will hide the MenuItem completely from the ActionBar and the Overflow Menu as well.

The difference might be when you set showAsAction to never, your menu item is still accessible with menu hardware or overflow button in ActionBar whereas when you use set visible method, your menu item is not displayed, even if you press the menu.
According to the reference, setVisible(boolean visible):
Sets the visibility of the MenuItem. If true the item will be visible; if false it is hidden (added in API 1)
And showAsAction method:
Sets how this item should display in the presence of an Action Bar (added in API 11)
As you can read, the mostly difference is showAsAction="never" is related to the ActionBar and hiding on it but still showing in overflow menu whereas setVisible since API 1 doesn't care about overflow menu and ActionBar: set to false, it litteraly hide your menu item.

Related

What is the difference between difference values of attribute showAsAction in item element in menus android?

What is the difference between the following in menus in android as I am getting the same output as when using never when using any of these:
1.withText
2.collapsActionView
Can anyone explain?
You have the answer on Google Developer site here.
withText for example, you can use in form android:showAsAction="always|withText" and it will try to show always text along with icon if there is room for it.
collapseActionView for example, you can use in form android:showAsAction="always|collapseActionView" to only show that item when menu is in collapsed view.
withText
Also include the title text (defined by android:title) with the action >item. You can include this value along with one of the others as a flag >set, by separating them with a pipe |.
collapseActionView
The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
Introduced in API Level 14.
There are five values associated with showAsAction. There differences are below.
ifRoom - only place an item in the app bar if there is room. If there are multiple items with "ifRoom" flag a few will be shown as actions in the app bar and the rest will be visible in an overflow menu.
withText - shows text defined in the title for the item without an icon. You can add one more flag using pipe "|". eg- "always|withText".
never- It will always show the item in the overflow menu instead of the app bar.
always- It will always show the item in the app bar. Avoid using this because if there are multiple items with the same flag it will overlap with other UI in the appbar.
collapseActionView- The action view associated with this action item (as declared by android:actionLayout or android:actionViewClass) is collapsible.
Introduced in API Level 14.
For more info visit menu-resource

How to know if MenuItem is in ActionBar or Overflow

First of, I'm using SherlockActionBar.
I've one MenuItem that is 'always' showed in the ActionBar and one other MenuItem that is only showed 'ifRoom'. There are also other MenuItems that are 'never' showed in the ActionBar and therefore there exists a Overflow Icon (if device has no hardwarebutton).
My question now is, how am I able to tell if the MenuItem with 'ifRoom' is currently showed or not in the ActionBar (in ActionBar or Overflow?)? I need to know that because I'm using the ShowcaseView Library to highlight these options. Therefore I have to know if I should highlight the Overflow or I can directly target to the MenuItem.
Thx
Imo there is no way to know if a menu entry is on the overflow menu via an API.
Even with "never" you can still be pushed into the overflow menu when the the title is long and your device is very narrow.
What you could try is to place an action view into your menu items and get its position on screen. If not on screen you can be certain it is pushed into the overflow menu.

Custom Overflow Menu Outside of ActionBar

I need to implement a custom overflow menu with menu items and the overflow icon if the screen size cannot show all the icons. I cannot use a side scroll since I am already inside an expanded list view. I cannot use the Top or bottom action bar menu because the action will change based on what list item has been expanded.
So I would like to create my own overflow menu - similar to the gmail screenshots attached
Any ideas? Apparently ABS can be used for this but I can't figure it out, please help :)
ListPopupWindow is what you're looking for. It's an API 11+ class, however. ABS includes a backported version which is basically just a ListView inside a PopupWindow (both API 1 classes).

Android menu on overflow BUT NOT not actionbar

any pointer as to how I could let my app running on android 3.0+ show the old menu ONLY on the overflow bar BUT not on the actionbar?
been trying to find this one out all over the web with no results!
any help is appreciated.
You can use the showAsAction attribute for MenuItem and set it to never to place the menu items in overflow menu.

SearchView which overlaps ActionBar

Instead of using an ActionLayout to replace the ActionBar when pressed a MenuItem. I want that ActionLayout to overlap on the ActionBar.
Since most of the applications use this behavior I thought its the standard SearchView widget.
And since my application is for API 10 and more, i couldn't use SearchView, hence used latest ActionBarSherlock library(4.2.0), where Jake added SearchView.
The problem is, even here its like replacing the initial menu items with the ActionViewClass.
The reason i want the ActionLayout to overlap but not replace the ActionBar is, I need to have 3 MenuItems in the ActionBar and if i keep all of them as
android:showAsAction="always|collapseActionView"
they are even visible when ActionLayout is shown, which gives little width for the ActionLayout
if i am using
android:showAsAction="ifRoom"
the last icon is missing. Only 2 icons are visible.
Thank You
If you are having physical menu button (e.g. Nexus S) in your device then the dot line will not show. When you press the Menu button it’ll show up.
You may use something as this Use android:showAsAction="never" to force an action item into the overflow.

Categories

Resources