user547995
user547995

Reputation: 2085

Menu doesn't appear

Hy!

I want to make my first Menue

I just createt the folder /res/menu and the file menu.xml

Code:

<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:id="@+id/quit"
          android:icon="@drawable/icon"
          android:title="Quit" />
</menu>

In my code i add:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.menu,menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
  Main.this.finish();
  return true;
}

If i start my app and press the menu button nothing appear.

Whats wrong?

Upvotes: 0

Views: 2930

Answers (3)

samser
samser

Reputation: 83

First you need to override onCreateOptionsMenu() insteald of onCreateContextMenu() and secondly, in the onOptionsItemSelected() you should do the following:

@Overrid
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.[your desired item id here]:
            //do what you want to do for this item!
        break;
    }
    return true;
}

Hope that helps!

Upvotes: 0

Samuel
Samuel

Reputation: 4387

Okay, Instead of your second group of code, try using this instead: this will fix your problem because you need to use onCreateOptionsMenu instead of onCreateContextMenu

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

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
         case R.id.quit: 
           finish();
           return true;
         default:
            return super.onOptionsItemSelected(item);
      }
}

Upvotes: 1

Cheryl Simon
Cheryl Simon

Reputation: 46844

You are doing things in onCreateContextMenu. This is the menu that comes up when you long press To add things to the menu you get hitting the menu button, you want to do them in onCreateOptionsMenu.

Upvotes: 0

Related Questions