Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to have a action bar menu using the appcompat library?

recently I've switched from the regular action bar implementation to the recently released appcompat implementation. My app made heavy use of the action bar to provide functionality. Since switching, on older spots APIs (less than 11) don't have any menu items. And newer APIs do, but they don't show the image like configured (if room|withText). Has anyone else experienced this or came up with any solutions?

like image 815
David Wood Avatar asked Nov 27 '22 21:11

David Wood


2 Answers

I found out what was up, when using the appcompat library. You can create your menu just like normal.

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

But, in your menu xml files, add a xmlns:app attribute to the menu tag, like so:

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

then, in each of your menu items, where you usually specify the "showAs" style (ifRoom, withText, etc.), include this alternative line alongside the regular one:

app:showAsAction="ifRoom|withText"
android:showAsAction="ifRoom|withText"

After this, your menus will show correctly on both current and old APIs. I got this information from here.

like image 132
David Wood Avatar answered Dec 04 '22 06:12

David Wood


If there is a physical "Menu" button on device, it will show the contextual menu. If not, the menu item will be added to the ActionBar.

like image 23
Androidea Avatar answered Dec 04 '22 07:12

Androidea