Reputation: 1965
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
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
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