user3242589
user3242589

Reputation: 51

Android - How to handle when the link is clicked on TextView via Linkify

I wanna do something additional act when the link is clicked on TextView.

The link on TextView is made via xml setting or using Linkify class. Like android:autoLink="true" or Linkify(textView, Linkify.WEB_URLS);

How do I catch the event when the link is click?

Upvotes: 4

Views: 481

Answers (1)

Harsh Modani
Harsh Modani

Reputation: 221

Rather than the Linkify library, use this code:

final boolean[] isClicked = {false};
final AppCompatActivity activity = getActivity();//or use 'this' if already in an activity 
textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        isClicked[0] = true;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(textView.getText().toString()));
        activity.startActivity(intent);
    }
});

Note that I've replaced your original boolean isClicked with boolean[] isClicked. This is because you can only use final variables in inner classes, and once isClicked is finalized you can't reassign it.

Upvotes: 0

Related Questions