0

Hi i'm trying to show the menu when the user presses the menu button. I'm using the code from the Documentation but the options menu won't show up. I guess i should have a listener for this menu button, but how?? This is my class so far:

    public class AppMenu extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }

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

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     // Handle item selection
     switch (item.getItemId()) {
     case R.id.hello:
         sayHello();
         return true;
     case R.id.bye:
      finish();
         return true;

     default:
         return super.onOptionsItemSelected(item);
     }
 }
}

Here is my xml file

    <?xml version="1.0" encoding="UTF-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/hello"
          android:title="Hello" 
          android:icon="@drawable/icon"/>
    <item android:id="@+id/bye"
          android:title="Bye" />
</menu>

Thanks !

madcoderz
  • 4,423
  • 10
  • 47
  • 74
  • Well from that point of description, nobody may help you, you should be more specific: what Android Version, which Context, some Code, which method did you used (xml?, Withhin Java). Anyway the menu button is always listening, except you overwrite the listener explicit. Be more specific and follow the stackoverflow Guide-lines. – joecks Dec 03 '10 at 11:18
  • @madcoderz: Post up your menu XML file. – Octavian Helm Dec 03 '10 at 12:08
  • I don't see any reason why it wouldn't work. – Octavian Helm Dec 03 '10 at 12:35
  • well that's sad because neither do i :( i debugged now and i noticed that this Activity is never called even if i press the menu button. Any suggestions? Can it be the emulator, something is wrong with it? – madcoderz Dec 03 '10 at 12:37
  • @madcoderz: How do you mean the activity is never called? Which activity? – Octavian Helm Dec 03 '10 at 12:42
  • the Activity or Class that has the onCreteOptionsMenu-method code posted above – madcoderz Dec 03 '10 at 12:46
  • @madcoderz: Don't you start that activity? What is the real problem here? You can't have the menu appear without having the activity loaded and running of course. – Octavian Helm Dec 03 '10 at 12:48
  • i thought that the menu button would trigger this activity automatically but it seems i was wrong. What listener could i use? – madcoderz Dec 03 '10 at 12:52
  • i don't think i should implement a listener from this activity because i want the user to call the menu from whatever activity is focused – madcoderz Dec 03 '10 at 12:54
  • So the real question is: Is there some meta data i can set up in Manifest.xml to make this happen? – madcoderz Dec 03 '10 at 12:57
  • @madcoderz: I'm highly confused. You can't have your menu appear from any other context than your own. – Octavian Helm Dec 03 '10 at 12:58
  • i am facing the same problem :p –  Mar 07 '16 at 21:03

1 Answers1

0

This answer is in response to the comment discussion on the question.

You can't have to menu appear outside of your Activity. That means you have to start your Activity and then from inside your Activity you'll be able to get the menu appear on a menu button press.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • Ok, so i started the activity, now i can see the menu ;) but as i move to the next Activity from a menu option, i can't open the menu again. Does this mean that i have to write Intent i = new Intent(); in every Activity? That's why i ask if there is a meta data that declares the menu Activity default for the whole app. Like we can do with searchable. Thanks!! – madcoderz Dec 03 '10 at 13:25
  • @madcoderz: Of course you won't be able to as you are in another Context when starting another Activity. – Octavian Helm Dec 03 '10 at 13:27