6

I have a textview with a html string with anchors in it. When I click the textview I want to call eg a method called A, and when I click a link in the textview I want to call a method called B. I got this working but I got a problem: when I click a link, method B is called, but method A is called too. How can I make sure only method B, and not B and A, is called when I click a link?

My code:

for (int i = 0; i < ingevoegd.length(); i++) {
        JSONObject soortingevoegd = ingevoegd.getJSONObject(i);
        String type = soortingevoegd.getString("type");         
        if (type.equals("Vis")) {
            String link = "<a href = 'com.aquariumzoeken.pro://Soortweergave?selected="
                    + naam + "&type=Vis" + "'>" + naam + "</a>";
            text = text.replaceAll(naam, link);
        }
    }

    TextView texttv = (TextView) v.findViewById(R.id.textviewer);   


    texttv.setText(Html.fromHtml(text));
    texttv.setMovementMethod(LinkMovementMethod.getInstance());

And the textview onclicklistener:

texttv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            try {

                switchToEditMode sw = new switchToEditMode();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

Thanks in advance, Simon

Simon
  • 2,328
  • 6
  • 26
  • 30
  • Have you tried enabling `android:autoLink="web"` for the textview and not attach the LinkMovement ? I think that should work. – Vrashabh Irde Jul 20 '13 at 15:56
  • Unfortunatelly that doesn't work. My links aren't clickable that way. – Simon Jul 20 '13 at 15:59
  • Can you attach a screenshot, im finding it hard to visualize the layout and why you have two events attached, is there empty space elsewhere in the TV and thats where you need Method B ? – Vrashabh Irde Jul 20 '13 at 16:03
  • I'll try to attach a screenshot. I got a textview with a text in it with some words as an anchor(so some words are a clickable link). And I want to call method A when I click a link, and method B when I click anywhere else on the textview – Simon Jul 20 '13 at 16:06
  • Oh ok I got it, dont bother :) – Vrashabh Irde Jul 20 '13 at 16:07

4 Answers4

10

I do the hack for you, try this code!

EXPLANATION:

1.use customized ClickableSpan to handle click on url.

2.clickablespan will handle the click event before the textview, make a flag when a link is clicked.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView textView = (TextView) findViewById(R.id.main_text);
    textView.setMovementMethod(LinkMovementMethod.getInstance());

    CharSequence charSequence = textView.getText();
    SpannableStringBuilder sp = new SpannableStringBuilder(charSequence);

    URLSpan[] spans = sp.getSpans(0, charSequence.length(), URLSpan.class);

    for (URLSpan urlSpan : spans) {
        MySpan mySpan = new MySpan(urlSpan.getURL());
        sp.setSpan(mySpan, sp.getSpanStart(urlSpan),
                sp.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
    }

    textView.setText(sp);

    textView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // 2.if clicking a link
            if (!isClickingLink) {
                Log.w("log", "not clicking link");
            }
            isClickingLink = false;
        }
    });
}


private boolean isClickingLink = false;

private class MySpan extends ClickableSpan {

    private String mUrl;

    public MySpan(String url) {

        super();
        mUrl = url;
    }

    @Override
    public void onClick(View widget) {

        isClickingLink = true;
        // 1. do url click
    }

}
kvh
  • 2,118
  • 19
  • 29
  • Worked like a charm. Thanks! I will just add that I have android:autoLink="web|phone|email" in my TextView xml and in the MySpan onClick I had to add an intent to go to the url. – alice_silver_man Dec 01 '14 at 23:56
  • I tried this and I get error: "URLSpan: Actvity was not found for intent". – trans Jul 19 '16 at 17:36
6

You could try modifying the onClickListener like mentioned here: Control onclicklistener in autolink enabled textview

texttv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                             if(texttv.getSelectionStart()==-1&&texttv.getSelectionEnd()==-1){ 

                       // Method B

                             }

                    }
                });

Basically check where and when links start and end and only if you arent over the hyperlink, then call your method B , otherwise it anyway fires A on the links But this is just a workaround I guess

Docs here: http://developer.android.com/reference/android/text/Selection.html#getSelectionEnd(java.lang.CharSequence)

Community
  • 1
  • 1
Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
0

After studying the source code, I've concluded that there is no good way to do this, but probably your best choice is a custom movement method. Something like this:

class MyMovementMethod extends LinkMovementMethod { 

    // Most of this code copied from LinkMovementMethod
    @Override
    public boolean onTouchEvent(TextView widget, Spannable buffer,
                                MotionEvent event) {
        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;
            } else {
                Selection.removeSelection(buffer);
                // NEW CODE - call method B
                B();
            }
        }

        return Touch.onTouchEvent(widget, buffer, event);
    }

}

Then instead of calling LinkMovementMethod.getInstance(), create a MyMovementMethod instead.

j__m
  • 9,392
  • 1
  • 32
  • 56
0

write this code in ClickableSpan

 ClicableSpan.onClick(View v){
     yourTextView.setOnClickListener(null);
     /*  do your own click
     */
     yourTextView.setOnClickListener(your listener);
 }