https://kotlinlang.org logo
#compose
Title
# compose
t

Travis Griggs

09/19/2022, 10:21 PM
Is there a way to get Compose Texts to autodetect clickable links in their text? Looking for something like iOS TextView url data detectors basically
e

ephemient

09/20/2022, 2:20 AM
there is no direct equivalent to android.text.util.Linkify/android:autoLink in Compose, but you can build your own without much trouble
HOWEVER. in general I would consider it much better to make the entire text clickable instead of just a small portion of it; the touch target is too small otherwise. Chrome has complex logic to try to snap touch inputs to the nearest link so that they are not so difficult to hit (https://chromium.googlesource.com/chromium/src/+/main/third_party/blink/renderer/core/page/touch_adjustment.cc), but Compose (and other Android code in general) does not
t

Travis Griggs

09/20/2022, 4:15 PM
unfortunately, the text content is a variable sized text and may or may not include hyperlinks. thanks for the code above!
I ended up changing the onClick to read:
Copy code
{ url ->
   var parsed = Uri.parse(url)
   if (parsed.scheme == null) {
      parsed = parsed.buildUpon().scheme("https").build()
   }
   startActivity(Intent(Intent.ACTION_VIEW, parsed))
}
So that it would follow "assumed scheme" links like the iOS TextView does. Is there a more elegant way to do that?
e

ephemient

09/20/2022, 7:37 PM
you could do that during the parsing,
Copy code
val url = match.value
var uri = Uri.parse(url)
if (uri.scheme == null) uri = uri.buildUpon().scheme("https").build()
withAnnotation(tag = "url", annotation = uri.toString()) {
or pull that out into a lambda
u

Uli Bubenheimer

11/02/2022, 10:48 AM
There is a related issuetracker request: https://issuetracker.google.com/issues/139312671
609 Views