gustavo
gustavo

Reputation: 141

SpannableStringBuilder set ClickableSpan more than once, can't click

I need to attach the same CharacterStyle to multiple regions, so I use CharacterStyle.wrap(). Then all regions are underlined but not clickable. What's the possible reason?

 ssb.setSpan(CharacterStyle.wrap(clickableSpan), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);

Upvotes: 3

Views: 690

Answers (1)

Floern
Floern

Reputation: 33904

This probably qualifies as a bug in the Android framework. The core issue is either the implementation of the method SpannableStringBuilder.getSpans() or the implementation of the LinkMovementMethod that should handle ClickableSpans.

The onTouchEvent() implementation of the LinkMovementMethod requests all ClickableSpan instances from the SpannableStringBuilder with getSpans() that filters its spans basically with instanceof. But problem is if you use CharacterStyle.wrap() your span is no longer a ClickableSpan instance, but a CharacterStyle.Passthrough instance instead. Therefore getSpans() does not return it anymore and it's no longer clickable.

Note that the text appearance (drawing) is not affected by this, thus it still looks like a ClickableSpan.

Now, to fix the problem you either have to extend SpannableStringBuilder to fix the getSpans() implementation (which is too complex), or extend the LinkMovementMethod.onTouchEvent() implementation to filter the ClickableSpan manually. You can find an example here that then can be used with

textview.setMovementMethod(new FixedLinkMovementMethod());

But then again, maybe it would be easier just to copy a span manually if its a ClickableSpan instead of using CharacterStyle.wrap().

Upvotes: 3

Related Questions