Show context menu on demand in Android

Consider Android ListViewwith the context menu configured with registerForContextMenu. The item’s context menu is ListViewdisplayed when the user long presses on the item’s view.

In addition, I would like to show a context menu when the user clicks (not long taps) on an element (if certain conditions are met). Is it possible to do this? How?

+3
source share
2 answers

I was able to solve this problem by viewing the Android source code. Here is what I did:

protected void onItemClick(AdapterView<?> adapterView, int position) {
    final int start = adapterView.getFirstVisiblePosition();
    final int index = position - start;
    final View childView = adapterView.getChildAt(index);
    if (childView != null) {
        adapterView.showContextMenuForChild(childView);
    }
}
+6
source

Yes call the executeLongClick method from your click event and it should do it

+1

Source: https://habr.com/ru/post/1778491/


All Articles