2

When the users clicks, it should open the webbrowser and go to https://www.google.com. But the textView's text should be "Click here for the site".

    textView.text = "Click here for the site"
    val pattern = Pattern.compile("Click here for the site")
    val scheme = "https://www.google.com"
    Linkify.addLinks(textView, pattern, scheme)

How to do this with Linkify?

This solution doesn't work: Android: Linkify TextView

AndroidManifest:

<uses-permission android:name="android.permission.INTERNET" />
Jim Clermonts
  • 1,694
  • 8
  • 39
  • 94

1 Answers1

0

First, I think the scheme value you have is incorrect (see documentation of the 3 parameter addLinks method for more). It should only be the URL scheme, not the entire URL. So, in you example, that would be:

val scheme = "https"

Although that probably still will not get you what you want since https://Click here for the site will not go anywhere. You probably need to add a TransformFilter and call the 5 parameter addLinks method. Something along the lines of (not tested, so syntax is probably off):

Linkify.addLinks(
    textView,
    pattern,
    null, // scheme
    null, // matchFilter
    new Linkify.TransformFilter() {
        public String transformUrl(Matcher match, String url) {
            return "https://google.com"
        }
    }
)

That is basically what is in Android: Linkify TextView that you said doesn't work in your case. So, finally or maybe firstly, you may need to add one or both of these lines:

textView.setLinksClickable(true)
textView.setMovementMethod(LinkMovementMethod.getInstance())

See the documentation on setLinksClickable and setMovementMethod.

See also my answer to a related question about clickable phone numbers.

John Cummings
  • 1,949
  • 3
  • 22
  • 38