0

Following the instructions at http://developer.android.com/guide/topics/ui/menus.html I try to add an option menu to my existing Activity by first creating an xml as

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

and then inflating the menu in the activity as

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.game_menu, menu);
    return true;
}

Apparently that is not sufficient. So what am I missing in the code? I am testing with Samsung S5.

My activity extends FragmentActivity, in case that matters.

Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • 2
    Did you try pressing the S5's menu key? Try pressing the multitasking key. Press and hold it in until and see if the menu pops up. – Antrromet Oct 01 '14 at 16:13

2 Answers2

1

The problem might be with the theme your activity is using. Try to use a theme which has actionbar in it. I suppose, Theme.Holo.ActionBar.

Manoj Seelan
  • 686
  • 5
  • 5
0

I'm sure the menu is there but you are not able to just see it. The above code, as far as I know, has no problem. The problem might be with the phones with a menu key and the ones without it. There have been infinite discussions over that here, here and here, and also here. One of the hacks that people use to force their menu options in the action bar overflow is

try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }

Put the above code in the onCreate() of your application class. You are using S5, so try long pressing the multitasking key. That enables the menu options in S5.

Community
  • 1
  • 1
Antrromet
  • 15,294
  • 10
  • 60
  • 75