Linkify in Android

Linkify is a helper class that creates hyperlinks within Text View (and Text View-derived) classes through RegEx pattern matching

Text that matches a specified RegEx pattern will be converted into a clickable hyperlink that implicitly fi res startActivity(new Intent(Intent.ACTION_VIEW, uri)), using the matched text as the target URI.

Native Linkify Link Types

The Linkify class has presets that can detect and linkify web URLs, email addresses, and phone numbers. To apply a preset, use the static Linkify.addLinks method, passing in a View to Linkify and a bitmask of one or more of the following self-describing Linkify class constants: WEB_URLS, EMAIL_ADDRESSES, PHONE_NUMBERS, and ALL.

TextView textView = (TextView)findViewById(R.id.myTextView);
Linkify.addLinks(textView, Linkify.WEB_URLS|Linkify.EMAIL_ADDRESSES);

You can also linkify Views directly within a layout using the android:autoLink attribute. It supports one or more of the following values: none, web, email, phone, and all.

<TextView
 android:layout_width=”match_parent”
 android:layout_height=”match_parent”
 android:text=”@string/linkify_me”
 android:autoLink=”phone|email”
/>

Leave a Comment