10

To enable Linkify behavior, I do this in code:

textView.setMovementMethod(LinkMovementMethod.getInstance());

Is there a way to set this in the layout xml for the textview?

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

3 Answers3

9

No, there is not. If you are using setMovementMethod(LinkMovementMethod.getInstance()); in so many places in your app that you want to avoid it, you should consider creating and using a custom class extending TextView which executes that method.

Cristian
  • 198,401
  • 62
  • 356
  • 264
1

Use the "autoLink" property in the XML layout

afollestad
  • 2,929
  • 5
  • 30
  • 44
0

In kotlin you can create an extension function like this

@SuppressLint("SetTextI18n")
fun TextView.makeHyperLink(url: String) {
    text = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml("<a href='${url}'>${text}</a>", Html.FROM_HTML_MODE_COMPACT)
    } else {
        Html.fromHtml("<a href='${url}'>${text}</a>")
    }
    movementMethod = LinkMovementMethod.getInstance()
    isClickable = true
}
Farhan
  • 3,162
  • 3
  • 25
  • 17