0

I am trying to make clickable links in text strings that launch an activity when clicked. I have used Linkify() to detect links in my text. This function is capable of adding links to text, but only web URLs. I need to turn my output into a ClickableSpan so that I can implement this technique.

How do I get Linkify()'s identified links to become ClickableSpans which direct to an activity?

Below is the code I have used in Linkify:

// Linkify parameters
final static Pattern pattern = Pattern.compile("\\[[^]]*]"); // defines the fact that links are bound by [square brackets]
final String scheme = "http://"; // TODO: Currently this is just a blank link
Linkify.addLinks(linkText, pattern, scheme);
Community
  • 1
  • 1
CaptainProg
  • 5,610
  • 23
  • 71
  • 116
  • I did something similar to this: [Format TextView to look like link](http://stackoverflow.com/q/11820526/1267661). If you combine the technique in your link with my `swapSpans()` method you should be able to do what you want. – Sam Sep 05 '12 at 22:55

2 Answers2

0

How do I get Linkify()'s identified links to become ClickableSpans which direct to an activity?

After your call to addLinks(), call getText() to retrieve the Spanned object from the TextView. This object will have a series of URLSpan objects, one per matched link -- you can get an array of those through a call to getSpans(). You will need to note where each those spans start and end (via getSpanStart() and getSpanEnd()), remove the URLSpan, and replace it with your own spans to do what you want.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

For what you want to achieve, it's probably simpler to just override the startActivity() method in your Activity and intercept the ACTION_VIEW intents with the URLs in your text. Something like this:

public class MyActivity extends Activity {

    @Override
    public void startActivity(Intent intent) {
        final String action = intent.getAction();
        if (action.equals(Intent.ACTION_VIEW)) {
            // launch our other activity instead
            Intent ourIntent = new Intent(this, MyOtherActivity.class);
            ourIntent.setData(intent.getData());
            super.startActivity(ourIntent);

            // we're done!
            return;
        }

        // else, normal handling by the framework
        super.startActivity(intent);
    }

    // the rest of your activity code

}

For reference, here's the source code for URLSpan which will trigger the startActivity() method above.

Joe
  • 14,039
  • 2
  • 39
  • 49
  • I've been looking at this for days now, and I simply can't work out what you're suggesting... Am I supposed to have this as a separate class that is never called, but which waits for a URL to be clicked on? – CaptainProg Sep 10 '12 at 22:52
  • As I understand it, I need to put this code at the start of my program, and have it wait for a URL to be clicked. My activity, when it is launched, takes a few variables. These are not available to this piece of code at the beginning of my program... or are they? – CaptainProg Sep 10 '12 at 22:58
  • The `startActivity()` method override above should be added to your `Activity` class (the one which displays the text that has been processed by `Linkify()`). – Joe Sep 11 '12 at 00:37
  • When you say _"My activity, when it is launched, takes a few variables"_ - are the variables passed in as extras in an `Intent`? If so, they should be accessible as usual to this `startActivity()` method by calling `getIntent()`. – Joe Sep 11 '12 at 00:41