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
}
I solved problem with this code line.