0

I'm trying to make an array of strings individually clickable within a text view that is within a RecyclerView (each tag would pass different data, which is fetched from the api on load). I've created the string using SpannableStringBuilder as below within the bindView method.

fun bindView(link: PostsModel)
     val tags = link.topics
     var spans = SpannableStringBuilder()

     for (tag in tags) {
         val string = SpannableString(tag.name)
         string.setSpan(ClickableTags(tag.name), 0, tag.name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
         spans.append(string)
     }
}

Then I set it to the text view.

view.headerTags.setText(spans, TextView.BufferType.SPANNABLE)

If I println() the contents of spans and view.headerTags.text, I can see it contains a string of tags, so it seems to be working. However, when testing in the app, it's not appearing in the text view.

enter image description here

If I set view.headerTags.text = "Tags should appear here", it works, so I'm not sure there's a problem with the text view.

enter image description here

Can't see to work out why it wouldn't appear, especially if the console is printing out the contents of text view? Can anyone let me know what I might be missing here?

Jason
  • 209
  • 2
  • 13
  • So looked like the text colour may have been transparent or yellow? Added ` string.setSpan(ForegroundColorSpan(Color.BLACK), 0, tag.name.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)` which seems to now work – Jason Jul 19 '19 at 11:09

1 Answers1

0

Please use

    Spannable word2 = new SpannableString("By signing in, I agree to The xxxxx\nxxxxxxx Terms of Service and Privacy Policy.");
    word2.setSpan(clickableSpan, 44, 60, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    word2.setSpan(clickableSpan1, 65, 80, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    textView.setText(word2);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setHighlightColor(Color.TRANSPARENT);

   ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(View textView) {
        Intent intent = new Intent(SignInActivity.this, ReadTermsOfServiceActivity.class);
        intent.putExtra("FROM", "termsservices");
        startActivity(intent);
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        super.updateDrawState(ds);
        ds.setUnderlineText(false);
    }
};
Umesh AHIR
  • 738
  • 6
  • 20
  • I need to use `SpannableStringBuilder` as I get back an array of tags, so I don't know what the string will be until `bindView()` is firing. – Jason Jul 19 '19 at 09:05