0

How can I write this switch statement? I want make two statements for the items below.

public boolean onPrepareOptionsMenu(Menu menu) {
switch(?????){
case ?????:

    }
}

MenuItem log = menu.findItem(R.id.action_Log);
log.setChecked(isChecked);

MenuItem sound = menu.findItem(R.id.action_Sound);
sound.setChecked(isChecked);
Laurel
  • 5,965
  • 14
  • 31
  • 57
Niclas
  • 254
  • 2
  • 9
  • so you want to handle the click event on menu items? Did you create the menu in xml and inflated in using `onCreateOptionsMenu`? – Leo Mar 29 '16 at 17:55
  • I want both items to be checked. – Niclas Mar 29 '16 at 19:37
  • your question was not clear, even after I asked you about it here in the comments. Why do you want to use a switch statement? – Leo Mar 29 '16 at 19:41
  • this questions addresses your porblem https://stackoverflow.com/questions/6683186/menuitems-checked-state-is-not-shown-correctly-by-its-icon – Leo Mar 29 '16 at 19:43

1 Answers1

1

If you want to know when the menu item is clicked you need to implement MenuItem.OnMenuItemClickListener in your Activity then override the method onMenuItemClick like this:

@Override
public boolean onMenuItemClick(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_Log:
            // do stuff
            return true;
        case R.id.action_Sound:
            //do stuff
            return true;
    }

    return false;
}
Leo
  • 834
  • 8
  • 14