ActionBar Sherlock Menu Item OnClick

G'sson picture G'sson · May 6, 2012 · Viewed 31.8k times · Source

I am new to using the Sherlock ActionBar and I have make it run in my app and I have a item in the actionbar to but I don't know how to make the item do something when it's clicked all I got is this.

public boolean onCreateOptionsMenu(Menu menu) {

    menu.add("Folder")
        .setIcon(R.drawable.folder)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    return true;
}


public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        //What do i write here?
    return true;

I hope you understand what I mean :)

EDIT

Hey I made it work with a little help from this thread that I found and I made a few changes and here is the code! :DDD

@Override
public boolean onCreateOptionsMenu(Menu menu) {
   com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.menu, menu);
   return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.search:
            finish();
            return true;
        case R.id.new_folder:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Answer

Hema picture Hema · May 18, 2012

Try this, it works:

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
      com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
      inflater.inflate(R.layout.menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
      // Handle item selection
      switch (item.getItemId()) {
      case R.id.settings:
          Intent i=new Intent(class1.this, clas2.class);
          startActivity(i);
          return true;
      }
      return false;
}