hello my friends :star-struck: :stuck_out_tongue: ...
# android
o
hello my friends đŸ€© 😛 A question please- I have a ClickableSpan() object that I am passing to a method I made named “setLinksWithinText”. ClickableSpan() has an abstract method “onClick”. I want to pass only the onClick implementation and not the entire ClickableSpan() object to the method i made. This is because ClickableSpan() also have a method updateDrawState and I want to set the updateDrawState in my setLinksWithinText method. This is how I instantiate the ClickableSpan() now:
Copy code
val customClickSpan = object : ClickableSpan() {
    override fun onClick(widget: View) {
        Toast.makeText(widget.context, "Click worked!", Toast.LENGTH_SHORT).show()
    }

    override fun updateDrawState(ds: TextPaint) {
        super.updateDrawState(ds)
        ds.color = Color.RED
        ds.isUnderlineText = true
    }
}
And then I pass it to my method:
Copy code
fun setLinksWithinText(stringResource : CharSequence, clickListener : ClickableSpan) : SpannableString {
    val stringResourceSpanned = stringResource as SpannedString
    val annotations = stringResourceSpanned.getSpans(0, stringResourceSpanned.length, android.text.Annotation::class.java)
    val spannableString = SpannableString(stringResourceSpanned)


    for (annotation in annotations) {
        // look for the span with the key "font"
        if (annotation.key == "text_type") {
            val textType = annotation.value

            if (textType == "link") {
                spannableString.setSpan(
                    clickListener,
                    stringResourceSpanned.getSpanStart(annotation),
                    stringResourceSpanned.getSpanEnd(annotation),
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
                )
            }
        }
    }

    return spannableString
}
Posted in #general