I have a simple demo app, where I use ClickableSpan to make a specific text as clickable. I works well when Talkback is OFF, of course.
But when Talkback is ON,
- If I use
rawText = shortRawText, it works fine - If I use
rawText = longRawText, 10 characters longer, then the onClick doesn't work
I've researched to fix it but can't find a solution for it (someone suggest to create separated TextViews for non-clickable and clickable space, but I think it's complex and not efficient).
Maybe anyone is interesting in or already met that issue can help me on this.
Video here: https://www.youtube.com/watch?v=FXnddfva34c
MainActivity.Kt
class MainActivity : AppCompatActivity() {
companion object {
const val START_CLICK_LABEL = "{start_link}"
const val END_CLICK_LABEL = "{end_link}"
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val shortRawText = "For more details {start_link}terms and conditions{end_link}"
val longRawText = "For more details, read our {start_link}terms and conditions{end_link}"
val rawText = longRawText
val linkText = rawText
.substringAfter(START_CLICK_LABEL)
.substringBefore(END_CLICK_LABEL)
val spannableString = SpannableString(
rawText.replace(Regex.fromLiteral(START_CLICK_LABEL), "")
.replace(Regex.fromLiteral(END_CLICK_LABEL), "")
)
val linkColor = Color.rgb(255, 0, 0)
val matcher: Matcher = Pattern.compile(linkText).matcher(spannableString)
if (matcher.find()) {
spannableString.setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
Log.d("HiepMT", "onClick() is called!")
}
override fun updateDrawState(textPaint: TextPaint) {
textPaint.color = linkColor
textPaint.isUnderlineText = false
}
},
matcher.start(),
matcher.start() + linkText.length,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE
)
}
// Set the movement method of the TextView to LinkMovementMethod
val textView = findViewById<TextView>(R.id.textView)
textView.movementMethod = LinkMovementMethod.getInstance()
// Set the text of the TextView to the SpannableString object
textView.text = spannableString
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>