Display menu items on the Android ICS action bar

I have an application that I wrote for Android 2.1 that uses the menu button to set some options. I would like this menu to be accessible through the overflow button of the action bar in ICS, but I am having problems displaying it.

If I change my target API to 15, the outdated menu button in the bottom panel will disappear in ICS, but the icon in the upper right corner of the action panel will not replace it, so there is no access to the menu, I tried to add the showAsAction attribute to the menu items that did nothing . I definitely customize 4.0.3 in my Eclipse build options.

I want this menu to be available somewhere using the ICS Holo theme, but still backward compatible with older devices. How should I do it?

+4
source share
2 answers

Usually you add these Action Items simply by implementing onCreateOptionsMenu(Menu menu) and adding android:showAsAction="ifRoom" for the desired items in the menu XML file.

eg.

 <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_save" android:icon="@drawable/ic_menu_save" android:title="@string/menu_save" android:showAsAction="ifRoom|withText" /> </menu> 

Did you consider this fact?

Edit: Here is a simple implementation that worked for me a while ago:

 @Override public boolean onCreateOptionsMenu(Menu menu){ super.onCreateOptionsMenu(menu); createMenu(menu); return true; } private void createMenu(Menu menu){ MenuItem mnu1 = menu.add(0, 0, 0, "Logout"); { mnu1.setAlphabeticShortcut('a'); mnu1.setIcon(R.drawable.icon); mnu1.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM); } } 
+6
source

I had this problem 5 minutes ago. I don’t know if this can be useful, but here is how I decided it:

Add super.setBooleanProperty ("showTitle", true); to onCreate method

 @Override public void onCreate(Bundle savedInstanceState) { super.setBooleanProperty("showTitle", true); super.onCreate(savedInstanceState); //more code... 

This will cause the action bar to appear, and a menu button replacement will appear. I aimed at android 4 in eclipse. Older devices only show the panel as a title and, of course, listen to the default action of the menu button.

0
source

Source: https://habr.com/ru/post/1393375/


All Articles