Clickable span consuming click when cursor is at the end of it

20 Views Asked by At

I am using clickable span in the edit text. When cursor is at the end of clickable span and keyboard is closed. When user click on the edittext insead of open the keyboard, clickable span consumed the click. Can anyone help how to solve this issue?

This is my code.

        val editText = findViewById<EditText>(R.id.editText)

    val text = "Click me!"
    val clickableSpan = object : ClickableSpan() {
        override fun onClick(widget: View) {
            // Handle the click event
            showToast("Clicked!")
        }
    }

    // Define the range for the clickable span
    val startIndex = 0
    val endIndex = 9

    // Apply the clickable span to the specified range
    val spannableString = SpannableString(text)
    spannableString.setSpan(
        clickableSpan,
        startIndex,
        endIndex,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
    )

    // Set the text to the EditText
    editText.setText(spannableString.toSpannable())

    // Make the clickable span work
    editText.movementMethod = LinkMovementMethod.getInstance()
0

There are 0 best solutions below