Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 684
thisiscrazy4 Avatar asked Jul 30 '13 21:07

thisiscrazy4


3 Answers

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);
}
like image 87
Jeff Hay Avatar answered Oct 14 '22 09:10

Jeff Hay


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);
    }
}
like image 33
Carlos Castro Avatar answered Oct 14 '22 07:10

Carlos Castro


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.

like image 1
Sourabh86 Avatar answered Oct 14 '22 09:10

Sourabh86