Search code examples
androidmenuactionbarsherlocklistenermenuitem

Why Is ActionBarSherlock Menu Not Working?


Im a noob to android development and i'm a having an issue making the menu item listener work. When i click on the menu item a toast is supposed to display but nothing happens. I have the correct imports and have implemented the menuitem listener so i don't understand why it is not working. Any help is greatly appreciated.

//ABS MenuItem import
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    menu.add(0,1,0,"Gender").setIcon(R.drawable.female_icon).setOnMenuItemClickListener(this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menu.add(0,2,0,"Flip").setIcon(R.drawable.flip_icon).setOnMenuItemClickListener(this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menu.add(0,3,0,"Preferences").setIcon(R.drawable.ic_action_example).setOnMenuItemClickListener(RemedyActivity.this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menu.add(0,4,0,"Help").setIcon(R.drawable.info).setOnMenuItemClickListener(RemedyActivity.this).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);


    //Side Navigation
    getSupportMenuInflater().inflate(R.menu.main_menu, menu);
    if (sideNavigationView.getMode() == Mode.RIGHT) {
        menu.findItem(R.id.mode_right).setChecked(true);
    } else {
        menu.findItem(R.id.mode_left).setChecked(true);
    }
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onMenuItemClick(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId())
    {
        case 1:   
            Toast mToast = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
            return true;  

        case 2:   
            Toast mToast2 = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
            return true;
    }
    return false;
}

EDIT

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            sideNavigationView.toggleMenu();
            break;
        case R.id.mode_left:
            item.setChecked(true);
            sideNavigationView.setMode(Mode.LEFT);
            break;
        case R.id.mode_right:
            item.setChecked(true);
            sideNavigationView.setMode(Mode.RIGHT);
            break;
        case 1:   // id from the xml file
            Toast mToast = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
            break;   // we handled the click, dont pass it up the chain

        case 2:   // id from the xml file
            Toast mToast2 = Toast.makeText(RemedyActivity.this, "works", Toast.LENGTH_SHORT);
            break;

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

Solution

  • I noticed that you are trying to show a toast, while using Side navigation. I've had some issues trying to show a toast with this library in the past. Try another method to determine whether or not the button is actually working such as NSLog.