Search code examples
androidlisteneronlongclicklistenerlinkify

onLongClick triggers Linkify onClick


I have a chat and I'm using Linkify to parse links and onLongClick to open a dialog that allows the user to copy the content of a message.

chatText is my TextView.

chatText.setText(message);
Linkify.addLinks(chatText, Linkify.ALL);
chatText.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
        final CharSequence[] items = {
                    "Copy"
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle("Select Action");
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                copyToClipboard();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
            return true;   
     }
    });

For normal messages it works fine, the problem is when I long click a linkifyed message it opens the dialog and then immediately follows the link. (e.g. opens the browser) When I get back to the app the form is still there and the copy to clipboards works just fine.

The

return true;

is supposed to block the onClick if I'm not mistaken. I can't figure out how to block the onClick whem i'm longclicking.


Solution

  • If anyone has the same problem, I found out it's a known issue of Android. One must use

        android:descendantFocusability="blocksDescendants"
    

    in order to avoid the linkifyed textview to block other events.

    Issue is reported here

    https://code.google.com/p/android/issues/detail?id=3414