I have a TextView that launches a context menu:
textView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu contextMenu,
View view,
ContextMenu.ContextMenuInfo contextMenuInfo) {
// Do stuff...
}
});
This textview also has the android:autoLink="all"
attribute set in its XML.
Now, if I set the contents of the TextView to a URL and long press over the URL, the context menu appears first, but when I lift my finger the link is pressed and opens the browser.
Is there any way to have the context menu or the long press consume the touch event so that the link is not clicked? I have considered overriding onTouch()
for the TextView to handle ACTION_UP events, but I cannot reliably keep track of when the context menu is visible to block the touch event.
This seems somewhat hacky to me, but it's the only way I've been able to make this work without directly handling the View's touch events (which may very well be the proper method).
We're essentially changing out the TextView's
LinkMovementMethod
if the context menu opens, so the up action doesn't fire the link. When the context menu closes, we restore theLinkMovementMethod
so that normal clicks on links work as expected.Adjust your
onCreateContextMenu()
method as follows:Then override the Activity's
onContextMenuClosed()
like so:Of course, this assumes that
textView
is a class member of your Activity.