ActionBarSherlock collapseActionView - android

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!

Related

Action Bar for Android being replaced by Menu Button?

Not sure if this is the appropriate place for this question, but here goes. I'm following the android tutorial for creating apps and I'm finding some issues with the implementation of the menus in the action bar. (http://developer.android.com/training/basics/actionbar/setting-up.html). I am testing on my Samsung Galaxy S4.
I am making an App for Android 3.0 and above but when I follow those instructions, the menu doesn't appear in the action bar, but becomes visible only when I press the menu button. If I follow the Android 2.1 and above instructions, the menu buttons appear in the action bar, except the overflow menu which is activated by pressing the menu button.
What do I need to do for Android 3.0 and above to make the menu appear in the action bar? Or is this no longer the way it works and it is meant to appear using the menu button instead now?
In menu.xml use showAsAction="always"
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_start"
android:showAsAction="always"
android:title="Start"/>
<item
android:id="#+id/action_signout"
android:showAsAction="always"
android:title="logout"/>
</menu>

Android menu item showAsAction="always" doesn't work

I have tried setting the:
android:showAsAction=".."
to every one of these:
ifRoom, never, withText, always, collapseActionView
but I always got the same result, which is not having any buttons on the action bar, so I have to press the 'menu' button.
Here is a picture of the menu now :
<item android:id="#+id/smth1"
android:title="#string/smth1"
android:showAsAction="always"
android:orderInCategory="1" />
I have even tried adding this:
android:uiOptions="splitActionBarWhenNarrow"
into application manifest file, but with no positive result (nothing changed).
I have tried running it on various kind of APIs (14, 16, 17, 19), but with the same result.
If my question seems to be unclear, here is a picture of a menu, which I would like to have:
Thanks for any help.
You need to use the compatibility namespace (see here)
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="#+id/menu_add_size"
android:title="#string/menu_add_item"
android:icon="#android:drawable/ic_menu_add"
app:showAsAction="always"/>
</menu>
Once you're using that you can use as many menu buttons as will fit. You're no longer limited to just two buttons + overflow showing.
You maybe just don't have enough space, you should first remove the title from your Activity by adding this to your onCreate method:
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
Then you can see here that the maximum number of icons you can display is directly linked to the width of your screen, on small phones you will only be able to see 2 icons in the ActionBar.

Should my Activity have menu as well as Action bar icons?

I am developing an application of my own and was reading this: http://developer.android.com/guide/topics/ui/menus.html
It says:
Beginning with Android 3.0, the Menu button is deprecated (some
devices don't have one), so you should migrate toward using the action
bar to provide access to actions and other options.
My app is only targeting android 4.2+. Does that mean I should present the menu options as only action bar icons ? What if there is not enough room available ?
Items that do not fit in the action bar will automatically be relegated to the overflow menu. All you have to do is set the android:showAsAction="ifRoom" property in your xml for the menu item.
This basically tells the system that this menu item should be displayed as an action icon if there is room available. If not, it should be shown as an overflow menu item.
It means that there is no more hardware Menu Button. Your menu items will be showed in popup under "three-doted icon". In menu.xml you can configure everything:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="#+id/action_example"
android:title="#string/action_example"
android:showAsAction="ifRoom" /> <!-- attention -->
<item
android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100"
android:showAsAction="never" />
</menu>
Read more here
If you need a lot of icons to be showed you can split ActionBar(second(bottom) menu bar will appear).
documantation of splitting
I hope mine explanation will be good enough:)

Getting the right icons for Action Bar (ActionBarSherlock) and Options Menu with Android Gingerbread

How do you manage Icons with ABS and Gingerbread? As you can see from the Screenshots, I have a menu item "Help", that sits in the action bar, when there's room for it. (when nothing is selected). The icon looks good so far. But when it moves into the options menu, I probably should use another icon for better visibility :)
How do you normally do that? Any ideas? This is my menu xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/new_button"
android:showAsAction="always"
android:title="new"
android:icon="#drawable/content_new_calendar"/>
<item
android:id="#+id/share_button"
android:visible="false"
android:showAsAction="ifRoom"
android:title="share"
android:icon="#drawable/social_share"/>
<item
android:id="#+id/help_button"
android:showAsAction="ifRoom"
android:title="help"
android:icon="#drawable/action_help"/>
</menu>
I know how to create different menus for various api levels, etc. But here, the same phone is showing the icon in the action bar and in the options menu. The simplest solution is to set android:showAsAction="never", but from user reviews I learned, that the App is difficult to understand (especially the Widget-Part), so I would love to have the help menu visible. Any ideas?
P.S. I know that the App is ugly and unfinished. It's work in progress and will be polished :)
Try using the force overflow option like so:
<item name="absForceOverflow">true</item>

why it only show 2 menu icon on the actionbar even though there are enough space for 3?

I have 3 menu icons on the menu bar, but everytime it only shows 2 icons, the last one is in nowhere.
my questions are:
1. there is enough space for 3 icons, why only 2 are shown?
2. if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
Below is my menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/refresh"
android:icon="#drawable/ic_menu_refresh"
android:showAsAction="ifRoom" />
<item android:id="#+id/add_homework"
android:icon="#android:drawable/ic_menu_edit"
android:showAsAction="ifRoom" />
<item android:id="#+id/set_groupid"
android:icon="#android:drawable/ic_menu_preferences"
android:showAsAction="ifRoom" />
</menu>
and this snippet is in my MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
I have 3 menu icons on the menu bar
For the purposes of this answer, I am assuming that by "menu bar" you are referring to the action bar.
but everytime it only shows 2 icons, the last one is in nowhere
The last one is available by pressing the MENU button, for devices (or emulators) that have an off-screen MENU button.
there is enough space for 3 icons, why only 2 are shown?
Presumably because Android disagrees with your assessment of whether or not there is enough space for 3 icons.
if the system thinks the space is not enough for the 3rd icon, why doesn't it combine the 2nd and 3rd icon into an overflow menu?
I have no idea why you think forcing the 2nd icon -- which, by your admission, fits -- into the overflow menu would be a good idea. The 3rd menu item is in the overflow menu, which is accessed via the MENU button on devices that have one or a three-vertical-dots button on the action bar for devices that lack a MENU button.
I encountered this same issue and resolved it by doing two things, although I think the main reason is the second item. I was using the Android Asset Studio to create icons for my Actionbar menu.
In Android Asset Studio, I set the icon to "trim".
In my menu/activity_main.xml definition file (or whatever you call your menu definition file) I defined my menu icons with android:showAsAction="always"
It looked something like this:
<item android:id="#+id/menu_test"
android:icon="#drawable/ic_menu_test"
android:title="#string/menu_test"
android:showAsAction="always" />

Categories

Resources