thisiscrazy4
thisiscrazy4

Reputation: 1965

Handle Linkify onClick events with own intent

I have a textview that contains emails and I want to be able to click on them to launch an activity within my own app. I am using Linkify.addLinks(TextView, Linkify.EMAIL_ADDRESSES) to make the emails clickable, however this pops up a chooser asking the user what app they'd like to open the email in. How can I directly handle this event so I can specify what activity gets called when clicking on a link?

Upvotes: 7

Views: 4529

Answers (3)

Jeff Hay
Jeff Hay

Reputation: 2645

Carlos' answer works well, but I use ACTION_VIEW for external intents I want to start as well. Fortunately, Linkify does add the application id as an extra to the intent (for Browser compatibility), which my other intents don't add, so I use the following logic to make sure I handle Linkify intents correctly:

@Override
public void startActivity(Intent intent) {
    boolean handled = false;

    if (TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {

        String app_id = intent.getStringExtra(Browser.EXTRA_APPLICATION_ID);
        if ( TextUtils.equals(getApplicationContext().getPackageName(), app_id) )
        {
            // This intent is a view coming from Linkify; handle internally
            //  << do something smart here >>
            handled = true;
        }
    }

    if (!handled)
        super.startActivity(intent);
}

Upvotes: 11

Carlos Castro
Carlos Castro

Reputation: 581

One way to accomplish this, is to override startActivity method on the activity that owns the TextView with the Linkify. Then, check the Intent action which should be ACTION_VIEW for Linkify Intent:

@Override
public void startActivity(Intent intent) {
    if (TextUtils.equals(intent.getAction(), Intent.ACTION_VIEW)) {
        // Your code here   
    }
    else {
        super.startActivity(intent);
    }
}

Upvotes: 7

Sourabh86
Sourabh86

Reputation: 744

You can use content providers and intent filters for this. Might have to write a regex for email address and then use that with Linkify. Here is an example of how to use custom regex and content providers with Linkify.

Upvotes: 1

Related Questions