Convert URLs to Links in TextView
To change the URLs to links in TextView widget, set the autoLink attribute with the value all or web.
Without the autoLink attribute set for the TextView, the Android UI displays URLs, emails, phone numbers, etc., just like any other text.
autoLink can take one or more of the following values.
- all – creates links for all the email, map, phone and web text.
- email – creates links for only email text.
- map – creates links for map data.
- none – creates links for none of the text.
- phone – creates links for phone numbers.
- web – creates links for web URLs.
<TextView
android:autoLink="all"
android:text="Welcome to https://koltinandroid.org/." />
Example
Create an Android Application with Kotlin support and Empty Activity. Create a TextView widget with text containing URL and autoLink attribute set to web or all.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_view_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="20sp"
android:autoLink="all"
android:text="Welcome to https://kotlinandroid.org/. Learn Kotlin Android." />
</LinearLayout>
MainActivity.kt
package com.kotlinandroid.myapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Screenshot
Click on the link.
You may choose the browser to open the URL.
Browser opens the URL.