setAutoLinkMask - Add Clickable Items in TextView, in Java

How to Add Clickable Items in TextView,  in Java

setAutoLinkMask was added in API Level 1
Sets the autolink mask of the text.

This is make the text in the TextView clickable items(email address, street addreses, phone numbers, web urls), and open the default application for each individual item.

NOTE: Try to set autoLink using XML instead when possible.

1. Add a TextView in your main.xml file.

2. Name the TextView myTextView.

3. Find the TextView using ID.

4. In the MainActivity.java, add the code below to the onCreate method

myTextView.setAutoLinkMask(Linkify.ALL);

5. At this point, the items in the TextView will still not be able to be clickable. To be clickable, one of these 2 options must be added after the setAutoLinkMask line of code. One or the other, both are not needed.

      a. Set the MovementMethod in a TextView

      b. Set the text in a TextView.

          myTextView.setText("This is an example. Url: androidsbs.blogspot.com Phone: 219-123-5555 Email: myemail@company.com Address: 1600 Amphitheatre Parkway Mountain View, CA 94043");

6. Compile and run!

Related Articles:
Resources: