Android Development
TextView
Hyperlinks
User Interface
App Design

How to make links in a TextView clickable

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Android development, enabling clickable links in a TextView is a useful feature when you want to display text that includes URLs, email addresses, phone numbers, or map addresses, making it easier for users to interact with these elements directly from the text. Below, we explore how to make links clickable in a TextView, including various customization options and considerations.

To create clickable links in a TextView, you can use the android:autoLink attribute in the TextView XML definition or handle it programmatically using Linkify in Java or Kotlin code. Here are the detailed steps for both methods:

  1. XML Configuration: In your layout XML file, add the TextView with the autoLink attribute set to the type of data you want to detect (web, email, phone, map, or all).
xml
1   <TextView
2       android:id="@+id/textViewLinks"
3       android:layout_width="wrap_content"
4       android:layout_height="wrap_content"
5       android:text="Visit our site: https://www.example.com or contact us at +1234567890"
6       android:autoLink="web|phone"
7       android:textColorLink="@color/link_color" />

In this example, both web URLs and phone numbers are made clickable.

  1. Handling Link Clicks: Links will be automatically detected and clickable. They will trigger the default action, such as opening a browser for web URLs or a dialer for phone numbers.

Programmatically With Linkify

  1. Java/Kotlin Implementation: You can also use the Linkify class to add link patterns programmatically. This is useful if you need more control over the link detection or if you are setting the text programmatically.
kotlin
   val textView = findViewById<TextView>(R.id.textViewLinks)
   textView.text = "Visit our site: https://www.example.com or contact us at +1234567890"
   Linkify.addLinks(textView, Linkify.WEB_URLS or Linkify.PHONE_NUMBERS)

This code snippet does essentially the same as the XML configuration but programmatically.

If you require custom behavior when a link is clicked, such as opening a dialog instead of navigating away from the app, you can implement a custom LinkMovementMethod.

  1. Custom Link Movement Method: Create a subclass of LinkMovementMethod and override the onTouchEvent method to handle link taps manually.
kotlin
1   class CustomLinkMovementMethod : LinkMovementMethod() {
2       override fun onTouchEvent(widget: TextView?, buffer: Spannable?, event: MotionEvent?): Boolean {
3           // Your custom code to handle links
4           return true
5       }
6   }
7
8   textView.movementMethod = CustomLinkMovementMethod()
  1. Handling Clicks Manually: When using custom handling, you'll typically check the URL or phone number clicked and perform the appropriate action.
MethodDescriptionUse Case
android:autoLinkAutomatically linkify text based on the type attribute.Simple cases where links open default actions.
LinkifyProgrammatically apply link patterns.When more control over link detection is needed.
Custom HandlerImplement LinkMovementMethod or use a clickable span.For custom actions on link click.

Best Practices and Additional Considerations

  • Performance: Automatically linkifying text can be expensive in terms of performance, especially for long texts in ListView or RecyclerView. Consider linkifying text data beforehand or during background processing.
  • User Experience: Always ensure that linked text is easily distinguishable from normal text (using different colors or underlining) and that it fits well into your app’s design.
  • Security: When linking to web URLs, consider the potential security implications, especially if the URLs are derived from user input. Always validate URLs to avoid linking to malicious content.
  • Accessibility: Make sure that your link text is clear and meaningful. Screen readers should convey the presence and purpose of links to visually impaired users. Use content descriptions if necessary.

By enabling clickable links in a TextView, you can enhance the user experience significantly, making your application more interactive and user-friendly. Whether you choose the simplicity of autoLink or the robustness of Linkify or a custom approach depends very much on the specific needs and context of your application.


Course illustration
Course illustration

All Rights Reserved.