Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of link text when clicked in a textview in android

I have a TextView. I have added custom links like "@abc", "#android" by matching some regex pattern. The links are displaying properly. However I am not getting a way to extract the text of the link which is clicked. I am using SpannableString to setText to the textview. I then set spans using my custom ClickableSpan. It works fine. Plus I can also catch the onclick event. But the onClick() method has a View paramter. If I call getText() on the View (ofcourse after typecasting it to TextView), it returns the entire text. I searched a lot but always found ways to add links and catch the event, but none told about getting the text of the link.

This is the code I am using to add links and recieve onclick. I got the code from one of the SO threads..

Pattern pattern = Pattern.compile("@[\\w]+");
Matcher matcher = pattern.matcher(tv.getText());//tv is my TextView
while (matcher.find()) {
    int x = matcher.start();
    int y = matcher.end();
    final android.text.SpannableString f = new android.text.SpannableString(
    tv.getText());
    f.setSpan(new InternalURLSpan(new View.OnClickListener() {
        public void onClick(View v) {
        showDialog(1);
    }
}), x, y, android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(f);
tv.setLinkTextColor(Color.rgb(19, 111, 154));
tv.setLinksClickable(true);

Here is the InternalURLSpan:

class InternalURLSpan extends android.text.style.ClickableSpan {
    View.OnClickListener mListener;

    public InternalURLSpan(View.OnClickListener listener) {
        mListener = listener;
    }

    @Override
    public void onClick(View widget) {
        mListener.onClick(widget);
        TextView tv = (TextView) widget;
        System.out.println("tv.gettext() :: " + tv.getText());
        Toast.makeText(MyActivity.this,tv.getText(),
        Toast.LENGTH_SHORT).show();
    }
}

Is it possible to get the text of the link clicked? If not, is there a way of associating some data to a particular link and knowing which link gets clicked? Any pointers.

Thanks

like image 688
Arunkumar Avatar asked Oct 11 '11 07:10

Arunkumar


3 Answers

Here is a pretty simple solution I found to get the value of the link inside the TextView when the user clicks on it. In this case I'm using phone numbers and it works like a charm.

myTextView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(myTextView.getSelectionStart()== -1 && 
                    myTextView.getSelectionEnd() == -1){
                Toast.makeText(getApplicationContext(), "You clicked outside the link", 
                        Toast.LENGTH_SHORT).show();
            }
            else {

                int start = myTextView.getSelectionStart();
                int end = myTextView.getSelectionEnd();
                String selected = myTextView.getText().toString().substring(start, end);

                Toast.makeText(getApplicationContext(), 
                        "Clicked: " + selected, 
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

Hope it helps.

like image 200
Alejandro Casanova Avatar answered Oct 14 '22 02:10

Alejandro Casanova


The solution goes like this -

Call setLinks() with you textview and the text to be added.

setLinks(textView, text);

setLinks() function is as -

void setLinks(TextView tv, String text) {
        String[] linkPatterns = {
                "([Hh][tT][tT][pP][sS]?:\\/\\/[^ ,'\">\\]\\)]*[^\\. ,'\">\\]\\)])",
                "#[\\w]+", "@[\\w]+" };
        for (String str : linkPatterns) {
            Pattern pattern = Pattern.compile(str);
            Matcher matcher = pattern.matcher(tv.getText());
            while (matcher.find()) {
                int x = matcher.start();
                int y = matcher.end();
                final android.text.SpannableString f = new android.text.SpannableString(
                        tv.getText());
                InternalURLSpan span = new InternalURLSpan();
                span.text = text.substring(x, y);
                f.setSpan(span, x, y,
                        android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tv.setText(f);
                // tv.setOnLongClickListener(span.l);

            }
        }
        tv.setLinkTextColor(Color.BLUE);
        tv.setLinksClickable(true);
        tv.setMovementMethod(LinkMovementMethod.getInstance());
        tv.setFocusable(false);
    }

and the InternalURLSpan class goes like this -

class InternalURLSpan extends android.text.style.ClickableSpan {
        public String text;

        @Override
        public void onClick(View widget) {
            handleLinkClicked(text);
        }

    }

handleLinkClicked() is as -

public void handleLinkClicked(String value) {
    if (value.startsWith("http")) {     // handle http links
    } else if (value.startsWith("@")) { // handle @links
    } else if (value.startsWith("#")) { // handle #links
    }
}
like image 43
Arunkumar Avatar answered Oct 14 '22 01:10

Arunkumar


Use

android:linksClickable="true" android:autoLink="web"

textView.setMovementMethod(LinkMovementMethod.getInstance())

like image 44
Muhammad Aamir Ali Avatar answered Oct 14 '22 00:10

Muhammad Aamir Ali