Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock items not appearing in overflow

I'm trying to restyle my app to an ICS look and feel with ABS. Getting the ActionBar itself was nice and straightforward, however adding menu items has not been.

My code looks like:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

All the appropriate imports are being used.

And my menu.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/backup" android:title="@string/backupLabel"/>
  <item android:id="@+id/restore" android:title="@string/restoreLabel"/>
</menu>

The ActionBar shows, but the menu behaves as a 2.1 menu - only activating from the menu button with no overflow available. This is also true on an ICS emulator - where I have to use the menu button to activate the menu.

If I add android:showAsAction="ifRoom" then the items appear as action items - but not in the overflow, which is where I would like them to always be.

Does that all make sense?

What am I missing?

like image 799
Paul Hunnisett Avatar asked Jun 30 '12 10:06

Paul Hunnisett


2 Answers

just use this code boath menu are on ActionBar:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/backup"
        android:showAsAction="ifRoom"
        android:title="backupLabel"/>
    <item
        android:id="@+id/restore"
        android:showAsAction="ifRoom"
        android:title="restoreLabel"/>
</menu> 

its look like:

enter image description here

Because if you are use

android:showAsAction="ifRoom" : Only place this item in the Action Bar if there is room for it.

and

android:showAsAction="Never" : Never place this item in the Action Bar.

if you use like :

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

        <item
            android:id="@+id/backup"
            android:showAsAction="ifRoom"
            android:title="backupLabel"/>
        <item
            android:id="@+id/restore"
            android:title="restoreLabel"/>
    </menu> 

then its look: one is on ActionBar & 2nd is open when you press menu button.

enter image description here

Note : Its working fine in < 3.0 Android OS so don't worry. But if you have multiple menu item(more then 3) with android:showAsAction="ifRoom" then it show you depend on device screen size.

in Protrait:

enter image description here

in Landscape:

enter image description here


After Doing all above i ll saggest you to use below way in < 3.0 Android os:

enter image description here

code:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/file"
        android:showAsAction="ifRoom"
        android:title="file">

        <!-- "file" submenu -->
        <menu>
            <item
                android:id="@+id/create_new"
                android:title="create_new"/>
            <item
                android:id="@+id/open"
                android:title="open"/>
        </menu>
    </item>

</menu>
like image 106
Dhaval Parmar Avatar answered Nov 15 '22 08:11

Dhaval Parmar


ForceOverflow has been removed in ABS 4.1.2 with the creator sighting that it should never have made it in (see the changelog notes). I am pretty confident you are seeing expected behaviour; any device with a menu button running ICS+ (e.g. my Nexus S) does not display the overflow menu since there is a dedicated hardware key - the 'menu' button.

Use the emulator configuration to remove the menu button from the emulated device and Android will know to add the overflow menu.

like image 36
BrantApps Avatar answered Nov 15 '22 08:11

BrantApps