Handling Multiple ClickableSpan in a TextView

I have been stuck in this problem for a long time. I have a simple line "This is a link and this is another link". I want the words "link" to be clickable to open different URLs in the browser.

  • The easiest way I can do is to set the Click-able Span mode on both “links” of the word with different URLs, but the problem I am facing is finding the start and end positions of the span. The text is dynamic, and I have to programmatically search for positions.
  • One approach is to find the first occurrence of the word 'link', find the start and end positions and set the range, and then the second occurrence. But this is unreliable. A text can contain more than one type of repeated words, for example "This is a cat link and this is another cat link". Here I need to link "cat" and "link", words with different URLs through a Click-able Span. How should I do it?
+4
source share
4 answers

Try this way

String s="Cat link 1 Cat link 2 Cat link 3";
    SpannableString ss = new SpannableString(s);
    String first ="Cat link 1";
    String second ="Cat link 2";
    String third ="Cat link 3";
    int firstIndex = s.toString().indexOf(first);
    int secondIndex = s.toString().indexOf(second);
    ClickableSpan firstwordClick = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ///............
        }
    }; 
    ClickableSpan secondwordClick = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            ///............
        }
    }; 
    ss.setSpan(firstwordClick,firstIndex, firstIndex+first.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    ss.setSpan(secondwordClick,secondIndex, secondIndex+second.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setLinksClickable(true);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setText(ss,BufferType.SPANNABLE);
+7
source

If you cannot programmatically find the difference in the links, then you cannot expect anything else to be able to do this. As a developer, you need a system.

- , , , . , .

URL-, , , , . .

0

- , /*/, , . , "", .

0

, orderId {b1j2gh4b}, bla bla sotre (1234124124) "

, orderID

 String notificationMessage = mArrListNotification.get(position).getMessage();
            boolean isPhoneNumberAvailable = false, isOrderIdAvailable = false;
            int phoneStartIndex = 0, phoneEndIndex = 0, orderIdStartIndex = 0, orderIdEndIndex = 0;

    //getting index on the basis of braces added to text
            if (notificationMessage.contains("(")) {
                isPhoneNumberAvailable = true;
                phoneStartIndex = notificationMessage.indexOf("(");
                phoneEndIndex = notificationMessage.indexOf(")");
            }
            if (notificationMessage.contains("{")) {

                orderIdStartIndex = notificationMessage.indexOf("{");
                orderIdEndIndex = notificationMessage.indexOf("}");

            }

            // we got the index so remove braces
            notificationMessage = notificationMessage.replace("(", " ");
            notificationMessage = notificationMessage.replace(")", " ");
            notificationMessage = notificationMessage.replace("{", " ");
            notificationMessage = notificationMessage.replace("}", " ");

            viewHolder.txtNotificationMessage.setText(notificationMessage, TextView.BufferType.SPANNABLE);
            Spannable mySpannablePhoneNumber = (Spannable) viewHolder.txtNotificationMessage.getText();
            Spannable mySpannableOrderID = (Spannable) viewHolder.txtNotificationMessage.getText();
            ClickableSpan mySpanPhoneClick = new ClickableSpan() {
                @Override
                public void onClick(View widget) {

                    currentPosition = (Integer) widget.getTag();

                    String message = mArrListNotification.get(currentPosition).getMessage();
                    int startIndex = message.indexOf("(");
                    int endIndex = message.indexOf(")");
                    phoneNumber = message.substring(startIndex + 1, endIndex);

     Log.i("Phone Number", phoneNumber clicked)

                }
            };
            ClickableSpan mySpanOrderClick = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    currentPosition = (Integer) widget.getTag();

                    String message = mArrListNotification.get(currentPosition).getMessage();
                    int startIndex = message.indexOf("{");
                    int endIndex = message.indexOf("}");
                    String orderID = message.substring(startIndex + 1, endIndex);
    // Log.i("Order id", orderID  clicked)

                }
            };
            if (isPhoneNumberAvailable) {
                mySpannablePhoneNumber.setSpan(mySpanPhoneClick, phoneStartIndex + 1, phoneEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            if (isOrderIdAvailable) {
                mySpannableOrderID.setSpan(mySpanOrderClick, orderIdStartIndex + 1, orderIdEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
0
source

All Articles