7

Should be simple enough but might not be.

When using action bar in Android 3.0+ you have the option of defining your menu items in XML or in code. I prefer to code them in xml as action bars feel more UI based than functional.

On an average day, you would use this to inflate the xml into a menu

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Menu is defined inside 'res/menu/...xml
    getMenuInflater().inflate(R.menu.activity_home, menu);
    return true;
}

And your XML file would look like this

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

    <item
        android:id="@+id/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/menu_settings"/>
    <item
        android:id="@+id/menu_item_menu"
        android:icon="@drawable/menu_off_128"
        android:showAsAction="ifRoom|withText"
        android:title="@string/inbox_string"/>
    <item
        android:id="@+id/menu_item_gallery"
        android:icon="@drawable/gallery_off_128"
        android:showAsAction="ifRoom|withText"
        android:title="@string/gallery_string"/>
    <item
        android:id="@+id/menu_item_inbox"
        android:icon="@drawable/inbox_off_128"
        android:showAsAction="ifRoom|withText"
        android:title="@string/inbox_string"/>
    <item
        android:id="@+id/menu_item_contact"
        android:icon="@drawable/phone_off_128"
        android:showAsAction="ifRoom|withText"
        android:title="@string/contact_string"/>

</menu>

Right now, I'm faced with the problem of making an actionbar backwards compatible and actionbarsherlock seems to be the most pleasant to use and popular. So I tried the above with actionbarsherlock and sadly there are compile time issues.

Namely that the Menu class returned by the inflater is from 'Android.view.menu' and not the 'com.actionbarsherlock.menu'. I went digging through the samples on github but all of them have the menu defined in code.

So has anyone manged to get an actionbarsherlock menu working with an XML file based layout?

Overtone
  • 297
  • 1
  • 5
  • 12

2 Answers2

23

try this

    @Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.your_menu, menu);
        return true;
}
Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
Georgy Gobozov
  • 13,633
  • 8
  • 72
  • 78
  • com.actionbarsherlock.view.Menu where i will get this class.. here is my link http://stackoverflow.com/questions/12779308/how-to-use-menu-in-layout of question. can i use this above example to my project. – Rao's Oct 08 '12 at 11:47
  • Are you guys able to test this with Robolectric by any chance? It looks like it can't inflate my menu from resource, giving out a Resources$NotFoundException. – Andrea Richiardi Jan 05 '14 at 01:09
5

Just had this problem myself.

What you want to to do is call getSupportMenuInflater() instead of getMenuInflater() like so:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
Droidweb
  • 51
  • 1
  • 3