views:

57

answers:

2

How can I open a context menu for a view without registering it using registerForContextMenu()?

My activity overrides onTouchEvent to detect motion, and if I use registerForContextMenu() it stops working. I'm detecting a long press, so I'd like to forward that request to a method that would build the menu for me, but don't know if it can work that way.

A: 

Its like "how can I drive without a car", right?

Is there a reason, why you dont want to use the registerForContextMenu() ?

Anyway, you could try to work with the onClick/onTouch listeners...

WarrenFaith
My activity overrides onTouchEvent to detect motion, and if I use registerForContextMenu() it stops working. I'm detecting a long press, so I'd like to forward that request to a method that would build the menu for me, but don't know if it can work that way.
JRL
A: 

I believe you can use View.setOnCreateContextMenuListener to manually bind your activity as the context menu provider for a view at layout initialization time, and then call View.showContextMenu on the view once you detect a long press to bring up the menu. It should call through to your onCreateContextMenu call just as if you'd registered it the normal way.

EDIT: Okay, this has the same effect of overriding the touch event listener for the view. Given that, the only solution I can think of is to create a hidden view and register to be that view's context menu provider, and then ask it to show a context menu as above when a long touch on the real/visible view is seen.

Walter Mundt
Tried it, it gives me same result as `registerForContextMenu()`, which is it breaks the motion detection. So the event must be consumed higher up in the chain than I want.
JRL
Hmm, that's too bad. Horrible-hackery option added to answer.
Walter Mundt