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?
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);
}
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);
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With