1

I'm writing an android application (sdk 8 to 15) and I'm dealing with the menu. I read a lot of things on it to understand, but I 'm stuck on one point :

Apparently since sdk 10, hardware menu button isn't supported, now we have to use the action bar. Ok. But in my app I want to have this kind of menu :

Bottom menu

Or this :

Another bottom menu

... which are not hardware menu button but seems to be 'native action bar'. All I can find on the web is an action bar on the top, with the title of the app and a logo on the left.. which take a lot of place. I'm so lost with all those sdk versions and I just want to integrate a simple menu like above, how should I proceed ?

I have this in my code, but :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_fragments_slider, menu);
    return true;
}

... but no menu appears with android 4 as it should be in the first or second image.

grena
  • 1,011
  • 1
  • 10
  • 25

2 Answers2

2

Check out the Menu Resource in the Android API Documentation and especially:

android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

The example XML from the documentation is a good resource also for exactally how to lay it out:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/item1"
          android:title="@string/item1"
          android:icon="@drawable/group_item1_icon"
          android:showAsAction="ifRoom|withText"/>
    <group android:id="@+id/group">
        <item android:id="@+id/group_item1"
              android:onClick="onGroupItemClick"
              android:title="@string/group_item1"
              android:icon="@drawable/group_item1_icon" />
        <item android:id="@+id/group_item2"
              android:onClick="onGroupItemClick"
              android:title="@string/group_item2"
              android:icon="@drawable/group_item2_icon" />
    </group>
    <item android:id="@+id/submenu"
          android:title="@string/submenu_title"
          android:showAsAction="ifRoom|withText" >
        <menu>
            <item android:id="@+id/submenu_item1"
                  android:title="@string/submenu_item1" />
        </menu>
    </item> </menu>

You then need to add to your Manifest:

android:uiOptions="splitActionBarWhenNarrow"

That will move your buttons down to the bottom, leave the title in the actionbar and if you have more than what can fit in the screen you get the little three dot | on the right of the lower actionbar. This only really works in Portrait mode though, unless you have quite a few menu items... or longer text strings (I think...)

jasonflaherty
  • 1,924
  • 7
  • 40
  • 82
  • Nice answer, but split ActionBar is not the option for this question. As far as I understood, the author wants it collapsed. – Yaroslav Mytkalyk Nov 30 '12 at 16:23
  • @DoctororDrive That second Img he's got makes it seem like he wants to have the icons, or not, on the bottom. He could hide them in that overflow bar | with android:showAsAction="never". – jasonflaherty Nov 30 '12 at 17:20
  • Thanks for your answers, very userful. I getting close to the wanted result but not completly, here is what I obtain : [image](http://box.grena.fr/images/device.png). The native app 'Gallery' have it for example : [gallery app](http://box.grena.fr/images/appgalerie.png) – grena Dec 03 '12 at 10:37
  • ah... I see. Hmmmm.... I am running 4.2 on my GNex and that option has been moved into the actionbar... – jasonflaherty Dec 03 '12 at 17:39
  • You're right, apparently the menu button in the second screenshot will disappear in the future, we finally chose your solution by adopting an action bar, thanks again ;) – grena Dec 04 '12 at 17:58
  • Glad that it will help you. I love that library! Enjoy the day. – jasonflaherty Dec 04 '12 at 18:45
1

ActionBarSherlock puts your menu stuff in the ActionBar across all supported platform versions. Add it as library project and make sure that you import the Sherlock Menu and extend SherlockActivity.

Yaroslav Mytkalyk
  • 16,950
  • 10
  • 72
  • 99
  • Yes but I'm getting an upper bar with the ActionBarSherlock, is there a configuration to put it near the others buttons like in the second image ? – grena Nov 30 '12 at 16:13
  • 1
    The second image is impossible. But you can add android:showAsAction="collapseActionView" on every menu item so they will appear collapsed in one button. The button will look like the second image but it will be placed in right corner of your Actionbar. The ActionBarSherlock demos have the "Collapsed" demo. – Yaroslav Mytkalyk Nov 30 '12 at 16:18