0

I have stared my eyes red on this:

I use ABS and everything works perfectly under Android 2.2, but on ICS item.getItemId() always returns 0. Since item.toString() returns different values. I feel I should be able to solve this, but I alway end up on the Info-page.

public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Info")
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    menu.add("Preferences")
        .setIcon(R.drawable.ic_preferences)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if(item.toString() == "Preferences"){
        Intent intent = new Intent();
        intent.setClass(this,Preferences.class);
        startActivity(intent);
        return true;
    }
    else {
        Intent intent = new Intent();
        intent.setClass(this,Info.class);
        startActivity(intent);
        return true;
    }
}

I guess I should switch on item.getItemId() instead, but I can't figure out where to define the ActionBar Views. For some time I have puzzled with a file actionbar.xml under the menu folder, but with little success:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/preferences" 
        android:showAsAction="always" 
        android:icon="@drawable/ic_preferences"></item>
    <item android:id="@+id/info" 
        android:showAsAction="always"></item>
</menu>

One should believe the the answer is rather simple. Can anyone give me a point in the right direction on an approach that works the same from SDK version 7 throught 15?

Any help is greatly appreciated.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
rudder
  • 87
  • 7
  • Follow the answer of Nick. As a note, remember that to compare Strings you have to use .equals() instead of ==. So, item.toString() == "Preferences" should be item.toString().equals("Preferences"). – YuviDroid Jul 07 '12 at 15:58

1 Answers1

2

Try this:

private static final int MENU_INFO = 0;
private static final int MENU_SETTINGS = 1;


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0, MENU_INFO, 0, "Info")
         .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    menu.add(0, MENU_SETTINGS, 0, "Preferences")
        .setIcon(R.drawable.ic_preferences)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return true;
}

/* Handles item selections */
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case MENU_INFO:
        startActivity(new Intent(this, Info.class));
        return true;
    case MENU_SETTINGS:
        startActivity(new Intent(this, Preferences.class));
        return true;
    }
    return false;
}

The trick is indeed to use item.getItemId(); as you can never be sure what exactly is returned on a .toString(). By assigning a static int-reference to each menu-item when you create the menu options, you know which one has been clicked in the onOptionsItemSelected-method.

Nick
  • 3,504
  • 2
  • 39
  • 78
  • So my lame try on the actionbar.xml is a dead end and the file can just be deleted? Thanks for the quick response, Nick. – rudder Jul 07 '12 at 16:04
  • This worked prefectly. Thanks a lot Nick. I will upvote your answer when my reputation is good enough :-) – rudder Jul 07 '12 at 16:11
  • Thanks :-)! I'm glad it worked :)! Yes, you can delete the actionbar.xml (it's also possible to define the menu in an .xml-file, but I find it easier (for small menus, at least) to do it directly in the Activity). I think you can "accept" my answer (using the green checkmark next to it) even without any reputation. – Nick Jul 07 '12 at 16:51