Search code examples
androidspannablestring

Android,setSpan twice?


Say I have a SpannableString that a URLSpan is set for span in it like this:

SpannableString ss = new SpannableString(text);
ss.setSpan(new URLSpan("com://my.app"), 3, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

Then I set another URLSpan for that span.But second URLSpan does not work and first one works.I can not use ss.removeSpan(what); because I do not want to remove all URLSpans.How I can solve this problem?


Solution

  • I use getSpans() in specific span(That I want to reset for it) to get it's span and then I use removeSpan to remove it:

    SpannableString ss = new SpannableString(text);
    ss.setSpan(new URLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    URLSpan[] toRemoveSpans = ss.getSpans(3, 6, URLSpan.class);
    ss.removeSpan(toRemoveSpans[0]);   
    ss.setSpan(new MyURLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);