Accessibility link on Android
Links should be accessible for users of assistive technologies. When accessibility is not taken into account, users might not be able to find or activate links.
On Android, links should be embedded inside an URLSpan
.
To create text links, you can show the span
in using the setText
method of TextView
. To support assistive technologies on lower version of Android, you need to call the ViewCompat.enableAccessibleClickableSpanSupport()
method.
The helper method ViewCompat.addLinks()
is also useful to automatically create accessible links.
Warning: you have to remove the android:autoLink
attribute from your XML to make your URLSpan
's clickable.
val textView = TextView(this)
val url = "https://appt.org"
val link = "Appt"
val spannableString = SpannableString("Learn more about $link")
val index = spannableString.indexOf(link)
spannableString.setSpan(URLSpan(url), index, index + link.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
textView.text = spannableString
textView.movementMethod = LinkMovementMethod.getInstance()
ViewCompat.enableAccessibleClickableSpanSupport(textView)