Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Menu with action bar sherlock

I need an example or a tutorial on how to add menu items with action bar sherlock

When I use the simple menu with the imports

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

when I call

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

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.goToSettings:
            startActivity(new Intent(this, SetPreference.class));
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }

    }

then I get the Cannot override the final method from SherlockActivity error.

like image 358
mkounal Avatar asked Aug 13 '12 09:08

mkounal


2 Answers

You have to use Menu, MenuInflater and MenuItem classes from com.actionbarsherlock.view package:

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.settings_menu, menu);

    return super.onCreateOptionsMenu(menu);
}

BTW, ActionBarSherlock contains a lot of samples.

like image 177
StenaviN Avatar answered Oct 06 '22 00:10

StenaviN


I used @StenaviN 's answer above but ran into problems with onContextItemSelected. This post solved it for me.

Basically, you just have to use

@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
    /* ... */
}

instead of

@Override
public boolean onContextItemSelected(MenuItem item) {
    /* ... */
}
like image 41
Matt Avatar answered Oct 06 '22 00:10

Matt