How to make links in a TextView clickable HyperLink on Android Kotlin/Java

To make a TextView clickable(hypertext link) in Android you can do it in value Strings file or calling setMovementMethod(). There are multiple scenarios and conditions.

Let’s see some situations which we have found on stack overflow:

A. android:autoLink="web" works if you have full links in your HTML. The following will be highlighted in blue and clickable:

  1. Some text 
  2. Some text 

B. view.setMovementMethod(LinkMovementMethod.getInstance()); will work with the following (will be highlighted and clickable):

  1. Some text 
  2. Some text 
  3. Some text 

Note:- that the third (A-3) option has a hyperlink, but the description of the link (the part between the tags) itself is not a link. android:autoLink="web" does NOT work with such links.

android:autoLink="web" if set in XML will override view.setMovementMethod(LinkMovementMethod.getInstance()); (i.e.; links of the third kind will be highlighted, but not clickable).

Summary: 

If using a view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and then make sure you don’t have android:autoLink="web" in your XML layout if you want all links to be clickable.

Let’s build an Android TextView link click application

Step 1. Create an android project in the android studio (Follow this tutorial: Android First Program in Android Studio).

Step 2. Open the strings.xml file.

make links in a TextView clickable

Step 3. Add this text in the strings.xml file.

Step 4. Add the following code in activity_mail.xml

Using a LinearLayout and 2 TextView fields for both examples.

Step 5. Add the below code in MainActivity.kt

Step 6. Now Run the application, in an emulator or on your Android device.

Output screenshot Android TextView link underline example:

Android TextView link underline

Download Complete Code TextView clickable

https://github.com/EyeHunts/AndroidTextViewClickable

Q: How to create a hyperlink in the Android TextView programmatically?

Answer: You can do it with this java code.

From API level >= 24 onwards Html.fromHtml(String source) is deprecated instead use fromHtml(String, int),

Or in the layout XML file, inside your TextView widget attributes

Do comment if you knew another way to do it or have any problem in this tutorial.

Note: This example (Project) is developed in Android Studio 3.3.2. Tested on Android 9 ( Android-P), compile SDK version API 28: Android 9.0 (Pie)
MinSdkVersion=25″
TargetSdkVersion=28″Coding in Kotlin


One thought on “How to make links in a TextView clickable HyperLink on Android Kotlin/Java

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.