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?
try this
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
getSupportMenuInflater().inflate(R.menu.your_menu, menu);
return true;
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With