1

I have a textView and a ListView. When the user clicks an element of the ListView, it will be added to the textView. I want to separate these strings on the TV, so when the user clicks on the specified string on the TextView I want to remove that element from the TV. I think I need SpannableString but I can't separate them from each other. I mean I can't perform different actions on the elements.

enter image description here

Derbi Senda
  • 15
  • 1
  • 6
  • Possible duplicate of [How to set multiple spans on a TextView's text on the same partial text?](http://stackoverflow.com/questions/14981307/how-to-set-multiple-spans-on-a-textviews-text-on-the-same-partial-text) – Okas Mar 12 '17 at 10:29

1 Answers1

0

If you already have your SpannableStrings you can try to concat them using TextUtils:

TextUtils.concat(span1, span2);

And to Create Spans you can use setSpan method;

setSpan(Object what, int start, int end, int flags)

,for Example to Create a ClickableSpan you can do this:

SpannableString styledString = new SpannableString(Text);
ClickableSpan clickableSpan = new ClickableSpan() {
                    @Override
                    public void onClick(View widget) {

                    }
                };
styledString.setSpan(clickableSpan, 0, Text.length(), 0);

P.S: Remember that you can set multiple span to your Spannable String, just be careful that the end of one span don't overlap the start of next span..

Keivan Esbati
  • 3,376
  • 1
  • 22
  • 36
  • For example how can I perform action on the second span when it has 6 spans? – Derbi Senda Mar 12 '17 at 19:42
  • Yes, as I mentioned above you can have as many SpannableString as you want, Also you can set Multiple Span to the same String using setSpan. Each span is created like this: setSpan(Object what, int start, int end, int flags) So you can even set what part of text is clickable and what part is normal text. – Keivan Esbati Mar 13 '17 at 05:14