Can't fire click event for coloredText

43 views Asked by At

I have method to create colored and clickable text inside textview. I can't fire onClick when click colored text. I can't see why it doesn't work.

MainFragment.kt

        textview.text = formatTextWithColorAndClickableSpan(
            requireContext(), getString(R.string.about_private_data_save), "Aydınlatma Metnini", onClickDescription
        )

private val onClickDescription = fun() {
        PermissionDialogType.PRIVATE_DIALOG  
 }

Method:

fun formatTextWithColorAndClickableSpan(
    context: Context,
    fullText: String,
    coloredText: String,
    onClick: () -> Unit
): SpannableString {
    val spannableString = formatTextWithColor(context, fullText, coloredText)

    val startIndex = fullText.indexOf(coloredText)
    val endIndex = startIndex + coloredText.length
    val clickableSpan = object : ClickableSpan() {
        override fun onClick(p0: View) {
            onClick()
        }

        override fun updateDrawState(ds: TextPaint) {
            super.updateDrawState(ds)
            ds.isUnderlineText = true
        }
    }
    spannableString.setSpan(clickableSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

    return spannableString
}

I tried these also:

      textview.text = formatTextWithColorAndClickableSpan(
            requireContext(), getString(R.string.about_private_data_save), "Aydınlatma Metnini", ::onClickDescription
        )

private fun onClickDescription() {
    PermissionDialogType.PRIVATE_INFO
}
1

There are 1 answers

0
Ahmet Yılmaz On

I solved problem with this code line.

    textview.movementMethod = LinkMovementMethod.getInstance()