How to make deep link string clickable in android TextView

Viewed 2146

How do I make a deep link string for example "myapp://product/123" clickable in android TextView. I know there are autoLink options like email, web and phone but there isn't any deeplink option. How do I make it clickable and launch the intent on click of that link?

4 Answers

Looking at https://stackoverflow.com/a/13509741/2914140, I wrote similar:

val url = "myapp://example.com/some_string"
textView.text = url
textView.setOnClickListener {
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
}

You don't even need <uses-permission android:name="android.permission.INTERNET" /> in AndroidManifest.

If you have an application, responding to myapp scheme and example.com host, it will be opened.

To format the textView like a link also write:

textView.hyperlinkStyle()


private fun TextView.hyperlinkStyle() {
    setText(
        SpannableString(text).apply {
            setSpan(
                URLSpan(""),
                0,
                length,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        },
        TextView.BufferType.SPANNABLE
    )
}
Related