Open In App

Linkify in Android using Jetpack Compose

Last Updated : 30 Mar, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Linkify Class in Android Operating System is used to create user-clickable links from the Text on the basis of pattern matching and regex. In simple words, Linkify observes the text, finds out if a span or whole text is a type of pattern, and converts it into a clickable link. For example, if the text is a sentence that consists of an email address, Linkify creates a link at that particular span. If this link is clicked, the application navigates to default available mailing applications. Linkify can identify and convert expressions like Email Addresses, Map Addresses, Phone Numbers, and Web URLs to clickable links.

Linkify in Android

In this article, we will show you how you could implement Linkify to a text in Android using Jetpack Compose. Follow the below steps once the IDE is ready.

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.

Step 2: Working with the MainActivity.kt file

Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.geeksforgeeks.jclinkify
  
import android.os.Bundle
import android.text.method.LinkMovementMethod
import android.text.util.Linkify
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.text.util.LinkifyCompat
  
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MainContent()
        }
    }
}
  
@Composable
fun MainContent() {
    Scaffold(
        topBar = { TopAppBar(title = { Text("GFG | Linkify", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
        content = { MyContent() }
    )
}
  
@Composable
fun MyContent(){
  
    val mContext = LocalContext.current
    val mCustomLinkifyText = remember {TextView(mContext)}
  
    val mText = "GeeksforGeeks\n" +
            "https://www.geeksforgeeks.org\n" +
            "5th & 6th Floor, Royal Kapsons, A- 118,\n" +
            "Sector- 136, Noida, Uttar Pradesh, India\n" +
            "feedback@geeksforgeeks.org\n" +
            "1800 258 4458"
  
    Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
        AndroidView(factory = { mCustomLinkifyText }) { textView ->
            textView.text = mText
            textView.textSize = 20F
            LinkifyCompat.addLinks(textView, Linkify.ALL)
            textView.movementMethod = LinkMovementMethod.getInstance()
        }
    }
}
  
@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    MainContent()
}


Output:

In the below video of this application, you can see that Linkify is able to identify and create links for all the types.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads