4

I have a text which has two clickable span text and the click action for these links working in normal mode. But in accessibility mode the textview gets the focus and click action on the clickable span text is not working.

Text used is given below and two clickable span text are "Privacy" and "Conditions"

<string name="hello_first_fragment">Hello first fragment Privacy and Conditions and other things</string> 

Layout used

     <?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=".FirstFragment">


    <TextView
        android:id="@+id/textview_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14sp"
        android:text="@string/hello_first_fragment"
        app:layout_constraintBottom_toTopOf="@id/button_first"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <Button
        android:id="@+id/button_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_first" />
</androidx.constraintlayout.widget.ConstraintLayout>

and inside the Fragment

val spannableString = SpannableString(getString(hello_first_fragment))

   
val clickableSpan1 = object : ClickableSpan() {
        override fun updateDrawState(textPaint: TextPaint) {
            // use this to change the link color
            textPaint.color = textPaint.linkColor
            // toggle below value to enable/disable
            // the underline shown below the clickable text
            textPaint.isUnderlineText = true
        }

        override fun onClick(view: View) {
            Toast.makeText(this@FirstFragment.activity, "Clicked Privacy", Toast.LENGTH_SHORT).show()
        }
    }
    spannableString.setSpan(
        clickableSpan1, 21, 28,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    val clickableSpan2 = object : ClickableSpan() {
        override fun updateDrawState(textPaint: TextPaint) {
            // use this to change the link color
            textPaint.color = textPaint.linkColor
            // toggle below value to enable/disable
            // the underline shown below the clickable text
            textPaint.isUnderlineText = true
        }

        override fun onClick(view: View) {
            Toast.makeText(this@FirstFragment.activity, "Clicked Conditions", Toast.LENGTH_SHORT).show()
        }
    }
    spannableString.setSpan(
        clickableSpan2, 33, 43,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    binding.textviewFirst.movementMethod =
        LinkMovementMethod.getInstance() // without LinkMovementMethod, link can not click
    binding.textviewFirst.setText(spannableString, TextView.BufferType.SPANNABLE)
Ranjith
  • 343
  • 1
  • 4
  • 17

0 Answers0