On an app that I’m working on, I need a context menu to show up if a user performs a longClick on a tab, which would allow them to close the tab. I can’t seem to find a way to add a listener to a tab though. I either need each tab to have its own listener or the listener needs to be able to tell which tab had the longClick performed on it, as it won’t always be the active tab.
Any ideas?
I appreciate that an answer has been accepted but if you want to utilise the built-in
ContextMenucapabilities rather than setonLongClickListenerson theTabWidgetitself, you can do this as follows…Example, my current
TabActivityadds tabs in aforloop and to register each for context menu I do the following.Then in my
ActivityI simply overrideonCreateContextMenu(...)andonContextItemSelected (MenuItem item)There’s no need to set an
OnLongClickListeneron any views explicitly as that is done by the call toregisterForContextMenu(...). Also, theContextMenucreation and selection handling is all handled for you by theContextMenumethods exposed byActivity.Unless you need to handle all of this stuff yourself (for a custom context menu layout for example) it seems easier to just use what’s buit-in to
Activity.