Crearting context menus

1,708 views
Skip to first unread message

Shaver Sport

unread,
Feb 16, 2012, 12:54:38 PM2/16/12
to ActionBarSherlock
Hey Jake,

I'm having a problem using the menuinflater with a contextmenu in one of my fragments.  The code is:

import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.Menu;
import android.view.ContextMenu;

  @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getActivity().getSupportMenuInflater();
        inflater.inflate(R.menu.mymenu, menu);
    }

The compiler complains saying MenuInflater takes a Menu, not a ContextMenu.  Is using the supportMenuInflater not supported with ContextMenus?

Thanks!






Jake Wharton

unread,
Feb 18, 2012, 4:51:28 PM2/18/12
to actionba...@googlegroups.com

For context menus you should use the regular menu inflater since it uses the regular menu classes.

Shaver Sport

unread,
Feb 18, 2012, 6:21:22 PM2/18/12
to actionba...@googlegroups.com
Thanks, Jake.  That did the trick.

Soups Ranjan

unread,
Jun 19, 2012, 1:16:52 AM6/19/12
to actionba...@googlegroups.com
Thanks for this question and the reply. I just wanted to add for others that you basically need to use native Android Menu/MenuItem/MenuInflater when using context menus to avoid issues.

public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {

...
android.view.MenuInflater inflater = (android.view.MenuInflater) getMenuInflater();
inflater.inflate(R.menu.list_context_menu, (android.view.Menu) menu);
...
}

public boolean onContextItemSelected(android.view.MenuItem item) {
}

-Soups

Will Cooper

unread,
May 15, 2013, 4:27:06 PM5/15/13
to actionba...@googlegroups.com
Just to be clear, for options menus we use sherlock classes, and for context menus we use androids?

Jake Wharton

unread,
May 15, 2013, 6:16:26 PM5/15/13
to actionba...@googlegroups.com

Yep.

On May 15, 2013 1:27 PM, "Will Cooper" <coolt...@gmail.com> wrote:
Just to be clear, for options menus we use sherlock classes, and for context menus we use androids?

--
You received this message because you are subscribed to the Google Groups "ActionBarSherlock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actionbarsherl...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply all
Reply to author
Forward
0 new messages