How to force overflow menu on android actionbar compat?
W

3

34

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.

Weanling answered 6/1, 2012 at 13:7 Comment(1)
From the ABS changelog CHANGELOG.md:27: * Fix: Remove .ForceOverflow themes. These never should have been included.Irrespective
P
47

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.

Partin answered 27/1, 2012 at 22:23 Comment(11)
You didn't read the question very carefully. He's using an official android action bar library to get the backwards and forwards compatibility. He wants to force the overflow option below Android 4.0, not turn his old menu into a functioning menu on ICS.Nerin
@EricNovins, ABS library offers what he is looking for. If he chooses not to use the library, if offers a direction for him to extend ActionBarCompat in his own way.Partin
I wouldn't ever suggest to ditch the library completely. That's a ton of recoding for one function. Not a quality answer IMONerin
But how could Google use action overflow on, let's say, Google+? I'm using a Nexus S, that has the hard menu button, so the overflow shouldn't be there...Anyway I see it on almost all stock gappsTedder
@Tedder Even though you have the hard menu button, the developer can force the overflow button to appear. This will just give a consistent UI across all devices, even if they are pre-ICS.Partin
@Amit How to force the overflow button? Check out the second paragraph of my edit.Partin
I have already added ActtionBarLib in my project... I have working actionBars and fragments in my project... I just wanted to ask force overflow menu button for few buttons...Armadillo
@Armadillo I believe you want to set your showAsAction attribute in the menu item element to "withText" for those you want to appear in the overflow.Partin
Does the menu "button" (not a physical, clicky, button like the home button) on the Samsung Galaxy S2 count as a hard menu button or a soft menu button? I don't get the overflow menu when using my S2 as a test device.Zambia
@JeffG Although the button doesn't press/click physically on that device, I believe that is still considered having a hard menu button.Partin
@esilac, is that mean, regardless either soft or hard Menu Button, that there can only be one Menu Button only?Bush
D
12

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. :)

Darrickdarrill answered 13/2, 2013 at 19:7 Comment(3)
this doesn't work. I couldn't make it show vertical overflow menu icon (3 vertical boxes which means: "click me to see the overflow menu"). They still go into the hardware menu buttonDurst
Great, for my app, your example it works perfectly!!Rigadoon
Brilliant! It's nice to have it for people that are used to old style. It's not as much of an issue for younger generation that can adapt to ever-changing technology, but the older folks have hard time figuring this stuff out. It's hard to explain to them that if there is a hardware button, they won't see a GUI button -- so it's just easier to implement it.Phlebotomy
R
6

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>

Rigadoon answered 30/10, 2013 at 13:3 Comment(3)
it works however when I add mention 'splitActionBarWhenNarrow' in android:uiOptions in activity it always displays the bar at bottom.Hillie
On skd 10 and below the overflow isn't shown as a dropdown menu but as a list in the center. Apart from that this methode works.Moisture
Seems like you must have the menu item as the SECOND item. If you have only it, alone, then it is displayed as an old menu, called from the hardware key.Glyn

© 2022 - 2024 — McMap. All rights reserved.