Ofir Bar
12/21/2019, 1:43 PMval 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:
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