Inflate the ActionBarSherlock menu defined in XML

It should be simple enough, but it may not be.

When using the action bar in Android 3.0+, you can define your menu items in XML or in code. I prefer to encode them in xml, because action panels perceive more of the user interface than functional ones.

On an average day, you would use this to inflate xml in 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 will 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 am facing the problem of making the taskbar backward compatible, and actionbarsherlock seems most enjoyable to use and popular. So I tried this with actionbarsherlock and, unfortunately, there are problems with compilation.

Namely, the Menu class returned by the inflatable device is "Android.view.menu" and not "com.actionbarsherlock.menu". I went through the samples on github, but they all have a menu defined in the code.

So, did anyone manage to get an actionbarsherlock menu that works with an XML-based layout?

+7
source share
2 answers

try it

  @Override public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { getSupportMenuInflater().inflate(R.menu.your_menu, menu); return true; } 
+23
source

I just had this problem.

What you want to do is call getSupportMenuInflater () instead of getMenuInflater () as follows:

 @Override public boolean onCreateOptionsMenu(Menu menu) { getSupportMenuInflater().inflate(R.menu.activity_main, menu); return true; } 
+5
source

All Articles