Skip to content

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 
    https://tutorial.eyehunts.com//
  2. Some text 
    https://tutorial.eyehunts.com//

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

  1. Some text 
    https://tutorial.eyehunts.com//
  2. Some text 
    https://tutorial.eyehunts.com//
  3. Some text 
    Go to EyeHunts

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.

<resources>
    <string name="app_name">Android TextView Clickable</string>
    <string name="text_link_ehs"> <a href="https://tutorial.eyehunts.com//">https://tutorial.eyehunts.com//</a></string>
    <string name="text_go_to"><a href="https://tutorial.eyehunts.com//">Go to EyeHunts</a></string>

</resources>

Step 4. Add the following code in activity_mail.xml

Using a LinearLayout and 2 TextView fields for both examples.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_link_ehs"
        android:padding="10dp"
        android:autoLink="web"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/textView_link"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text_go_to"
        android:padding="10dp"
        android:textSize="18sp"/>

</LinearLayout>

Step 5. Add the below code in MainActivity.kt

package com.eyehunts.androidtextviewclickable

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.method.LinkMovementMethod
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textView_link.setMovementMethod(LinkMovementMethod.getInstance())
    }
}

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.

TextView textView =(TextView)findViewById(R.id.textView);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
String text = "<a href='http://www.google.com'> Google </a>";
textView.setText(Html.fromHtml(text));

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

textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));

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

android:autoLink="web"
android:linksClickable="true"

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

2 thoughts on “How to make links in a TextView clickable HyperLink on Android Kotlin/Java”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading