Fresheyeball
Fresheyeball

Reputation: 30015

android menu does nothing

We are targeting android 2.2+

I am trying to use androids built in system regarding the hardware menu button still found on many phones. However I have had no luck and no error message:

/res/menu/optionsmenu.xml

<?xml version="1.0" encoding="utf-8"?>

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menuHelp"
          android:title="Help"
          />
    <item android:id="@+id/menuShowInstallationCode"
          android:title="Show Installation Code"
          />
</menu>

in the activity class

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


public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.menuHelp :
            return true;

        case R.id.menuShowInstallationCode :
            return true;

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

The result so far is, that I press the menu button and nothing happens. Really nothing, no error message, no menu, nothing.

I would like a menu to appear. Anyone have any ideas?

Upvotes: 1

Views: 873

Answers (2)

Marcin S.
Marcin S.

Reputation: 11191

You should return true in onCreateOptionsMenu() and return super.onOptionsItemSelected(MenuItem item) in onOptionsItemSelected(MenuItem item) after switch statement. Please change to:

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


public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case R.id.menuHelp :
            return true;

        case R.id.menuShowInstallationCode :
            return true;

    }
    return super.onOptionsItemSelected(item);
}

Upvotes: 1

simsam7
simsam7

Reputation: 74

Here's a complete code piece, tested on Android 2.2. I didn't create any XML for it, not necessary.

package com.samplemenu;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    static final int MENU_ONE = 0;
    static final int MENU_TWO = 1;    


    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, MENU_ONE, 0, "One").setIcon(R.drawable.ic_launcher);
        menu.add(0, MENU_TWO, 0, "Two").setIcon(R.drawable.ic_launcher);
        return true;
    }   

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case MENU_ONE: {

        }
        return true;

        case MENU_TWO: {

        }
        return true;

        }
        return false;
    }

}

Upvotes: 1

Related Questions