I have a ListView , I need to select an item in it and, when I click on a menu item, do actions. In this case, everything should be beautiful (so I chose the context menu, rather than dialog activity, as before) and the handler should get the position of the clicked item.

I came to the option to handle a long click on the listener with setOnItemLongClickListener , and in the onItemLongClick method onItemLongClick call a context menu to which I can transfer the position of the clicked item. But I ran into the problem that when the button is clicked, the context menu is called - no! The easiest option was to create a button (programmatically, without a layout file) and click on it. Initially, I wanted to call the onCreateContextMenu method, but there were a lot of arguments and I just don’t know where they come from, or how to manage them or how to replace them.

So, what is the question: how to programmatically make a long press on the button to bring up the context menu?

  Не люблю я такую бяку делать, но без нее никак :)). 
  • If you put a longClick listener on the list, then no special button is needed. The listener will work with a long click on the item's list itself. The same with ContextMenu linked to the list - it will be called by itself with a long click on the item - neither need be called explicitly by any command. - pavlofff
  • I should also note that ignorance, how to do something correctly, does not justify the use of some crutches, hidden buttons and other, let's say, non-standard implementations. It is necessary to learn to do correctly, to understand, and not to invent "the simplest options", especially since onCreateContextMenu receives only three parameters, each of which is perfectly documented. Also, it does not need to be called up from somewhere - it will be called up with a long tap on the item. - pavlofff
  • The context menu is basic functionality, everything just works with a long tap and just gets the positions of this tap. If you are wrong and you do not know why you are not like everyone else - create a question about this problem, and not about crutch solutions to bypass the proper functionality. - pavlofff
  • @pavlofff, I already found how to do it :). - user189127

0