How to force overflow menu on android actionbar compat? - android

Android action bar compat
Is it possible? On older devices (pre 3.0) the items that don't fit the action bar are only shown when the menu key is pressed, I want these items to be grouped in the actionbar's overflow menu.

The action overflow menu is only available when there is no hard menu button available on the device. I found this stated in the Framework Topics under User Interface > Action Bar, check out the 3rd bullet here.
There is an action bar library written by Jake Wharton called ActionBarSherlock. Perhaps this is able to supply you with an action overflow menu style even when on older devices (which include a hard menu button), however I have not looked into this.
Edit: ActionBarSherlock 4.0 (currently a release candidate) has functionality built in to force action overflow. If you want to extend the ActionBarCompat example yourself, you could take a look on github to get an idea how Jake implemented it. I would suggest just looking into using his library all together, as it is very well done.
If you choose to use Jake's library, look into setting up the Activity theme as #style/Theme.Sherlock.ForceOverflow to force the overflow menu on older devices.
Edit2: Using ForceOverflow theme causes issues (example #1) on devices with hardware menu button. Thus, Jake Wharton is going to remove ForceOverflow in the future versions.

Okay, this is simple but hard to figure out.
You first need a menu item you want to use as the overflow inflater. Example
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
</item>
Once you have your item, add a sub-menu containing your items you want in the overflow menu. Example:
<item
android:id="#+id/a_More"
android:icon="#drawable/more"
android:showAsAction="always"
android:title="More">
<menu>
<item
android:id="#+id/aM_Home"
android:icon="#drawable/home"
android:title="Home"/>
</menu>
</item>
On click this will inflate other items within. My application is using ActionBarSherlock 4.0 so before this will work for you, you will need to access the "SplitActionBar". (Will still work on default android Actionbar)
Here's how:
In your AndroidManifest.xml file, you need to add this code under the activity you need the overflow menu in.
Honestly it shouldn't matter if you have the actionbar split or not but I prefer it.
android:uiOptions="splitActionBarWhenNarrow"
NOTE: Your item that inflates your overflow menu MUST showAsAction="always"
Vwola! you have an overflow menu! Hope I helped you out. :)

Following LeviRockerSk8er's suggestion I've forced to have a overflow menu in the action bar like this:
This is the code for "menu.xml":
<item
android:id="#+id/web_clasica"
android:icon="#drawable/ic_action_web_site"
android:showAsAction="ifRoom"
android:title="#string/menu_web"
/>
<item
android:id="#+id/overflow_fijo"
android:icon="#drawable/ic_action_core_overflow"
android:showAsAction="always"
android:title="#string/menu_email"
>
<menu>
<item
android:id="#+id/email"
android:icon="#drawable/ic_action_new_email"
android:showAsAction="ifRoom"
android:title="#string/menu_email"
/>
<item
android:id="#+id/share"
android:icon="#drawable/ic_action_share"
android:showAsAction="ifRoom"
android:title="#string/menu_share"
/>
<item
android:id="#+id/about"
android:showAsAction="ifRoom"
android:icon="#drawable/ic_action_action_about"
android:title="#string/menu_about"/>
</menu>

Related

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.

What does overflow mean in this case?

I came across this during my android training :
Specify the Actions in XML
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:showAsAction="never" />
This declares that the Search action should appear as an action button when room is available in the action bar, but the Settings action should always appear in the overflow. (By default, all actions appear in the overflow, but it's good practice to explicitly declare your design intentions for each action.)
what does overflow mean in this context? It seems to me some design construct. Please explain.
Thank you in advance.
Overflow is a menu item which groups menu items that are not immediately visible on the ActionBar in a separate menu which needs to be tapped to show the contents. Refer to the Action Bar documentation for more information.
In the image above, #3 represents the overflow menu. The documentation on Menus also has quite a lot of information about this.

Text or Icon in action bar, stuck with the 3 dots

I'm making an android app in Eclipse. I want to place a text or icon in the action bar, but when I am writing in menu.xml, which looks like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/createnew"
android:orderInCategory="1"
android:showAsAction="ifRoom"
android:title="CREATE"/>
</menu>
it puts it in the options menu. I want to separate it.
I see this in the app when I run it:
(sorry can't post pictures because I don't have enough rep)
I want to CREATE next to the option menu like this for example:
Since you are using the AppCompat Actionbar, you need to use a custom namespaced showAsAction attribute.
It should look something like this:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/createnew"
android:orderInCategory="1"
yourapp:showAsAction="ifRoom"
android:title="CREATE"/>
</menu>
Note that this uses yourapp:showAsAction instead of android:showAsAction. This is because the showAsAction attribute is not available on pre-Honeycomb devices and is provided by the support library.
For more information, read the Action Bar developer guide.
It's caused by this line
android:showAsAction="ifRoom"
That tells Android to put it up there if it's going to fit nicely. If you want it up there no matter the fit use
android:showAsAction="always"

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>

Categories

Resources