2

I have a ListView with a linkify enabled TextView. The problem that I am facing is while the user scrolls the list, by mistake if he touches the textiview it open ups the link.

Can I interrupt this click event?

Or is it possible to remove the touch and enable only click events for linkify added textviews. ?

Looking forward for suggestions.

Navya Ramesan
  • 191
  • 2
  • 10
  • Yes, you should use onClickListener for your TextView rather than onTouch. – Carnal Jan 28 '15 at 12:53
  • remove touch and add manual click event – M D Jan 28 '15 at 12:53
  • The onClick would be affected for the whole textview. I just needed the click event only for the link. Lets say I have a text as "Lorem ipsum dolor sit amet, consectetuer ullamcorper mollis. Sit totam ut ut sit quisque, www.google.com lectus quam laoreet, nam morbi interdum habitasse " Only the google.com should get the click event. not the whole textview. because the listview already has the item click event. – Navya Ramesan Jan 28 '15 at 12:56
  • also when i try to click that link, it gives me two suggestion popups. If I am right, I am getting two events, one is the onTouch and other is onClick. – Navya Ramesan Jan 28 '15 at 12:59

1 Answers1

3

the solution to my problem is after Linkify to remove the textView's scrolling method and handle the LinkMovementMethod link detection action in onTouchEvent of the textView.

@override
public boolean onTouchEvent(MotionEvent event) {
    TextView widget = (TextView) this;
    Object text = widget.getText();
    if (text instanceof Spanned) {
        Spannable buffer = (Spannable) text;
        int action = event.getAction();

        if (action == MotionEvent.ACTION_UP
                || action == MotionEvent.ACTION_DOWN) {
            int x = (int) event.getX();
            int y = (int) event.getY();

            x -= widget.getTotalPaddingLeft();
            y -= widget.getTotalPaddingTop();

            x += widget.getScrollX();
            y += widget.getScrollY();

            Layout layout = widget.getLayout();
            int line = layout.getLineForVertical(y);
            int off = layout.getOffsetForHorizontal(line, x);

            ClickableSpan[] link = buffer.getSpans(off, off,
                    ClickableSpan.class);

            if (link.length != 0) {
                if (action == MotionEvent.ACTION_UP) {
                    link[0].onClick(widget);
                } else if (action == MotionEvent.ACTION_DOWN) {
                     Selection.setSelection(buffer,
                             buffer.getSpanStart(link[0]),
                             buffer.getSpanEnd(link[0]));
                }
                return true;
            }
        }

    }

    return false;
}

This way i have the Link_Click detection (performed only with the user touches the link and not the whole textview) and i don't have the whole LinkMovementMethod.