Opening a floating contextual menu by using GestureDetector onLongPress [ANDROID]

I have a fragment implementing onCreateContextMenu (where the menu layout is inflated) and onContextItemSelected menu, and I set have set an addOnItemTouchListener on my RecyclerView to detect long-press. I created a GestureDetector instance in this listener and override the onLongPress method. I try to test whether the event is captured (by printing a toast message), and it works. However, when I’m trying to show the contextual menu when there is a long press by using this touch listener (my reasoning is because I want to intercept the listener so it the event won’t propagate to the viewgroup’s children), it doesn’t show any contextual menu after longpress.
I have also applied registerForContextMenu(recyclerView), also tried applying
openContextMenu(recyclerView) but the compiler detects there is no method with such declaration.

Answer

Ok, apparently I’m calling the openContextMenu from a fragment and the method’s only available in Activity. So in the end I use getActivity().openContextMenu and it works.

Leave a Reply

Your email address will not be published. Required fields are marked *