The whole point of a context menu is that it is associated with an individual underlying view, and it is clearly a design limitation in Android that the association is lost in the callback 'onContextItemSelected'. Enabling long-touch on any view of sufficient size seems perfectly reasonable as an alternative to a right mouse click.
As other posts have recommended, for some contexts:
AdapterView.AdapterContextMenuInfo menuInfo =
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
is appropriate and the targetView is a useful reference point.
Another way is to subclass the view and override 'getContextMenuInfo' to provide the view reference. For example, a simple TextView:
package ...;
public class TextViewWithContext extends TextView {
TextViewContextMenuInfo _contextMenuInfo = null;
public TextViewWithContext(Context context) {
super(context);
_contextMenuInfo = new TextViewContextMenuInfo(this);
}
public TextViewWithContext(Context context, AttributeSet attrs) {
super(context, attrs);
_contextMenuInfo = new TextViewContextMenuInfo(this);
}
protected ContextMenuInfo getContextMenuInfo() {
return _contextMenuInfo;
}
public boolean isContextView(ContextMenuInfo menuInfo) {
return menuInfo == (ContextMenuInfo)_contextMenuInfo;
}
protected class TextViewContextMenuInfo implements ContextMenuInfo {
protected TextView _textView = null;
protected TextViewContextMenuInfo(TextView textView) {
_textView = textView;
}
}
}
...
@Override
public boolean onContextItemSelected(MenuItem item) {
ContextMenuInfo menuInfo = item.getMenuInfo();
if (textViewWithContext.isContextView(menuInfo) {
...
}
}
Finally, it would have been more helpful if the base View class had assigned a ContextInfo object with a reverse reference to the view, rather than null as at present.