Search code examples
androidtextviewspannablestring

Getting the range of an applied span in SpannableString


I am trying to get the set of applied spans on a SpannableString and then find the ranges in which they have been applied. Is that possible? As far as I have seen, there doesn't seem to be a built in method to do this. Am I right?

Object[] spans = spannableString.getSpans(0, spannableString.length(), Object.class);

    List<Object> spanArray = asList(spans);

    for (Object span: spanArray) {
        if (span.getClass().equals(MyCustomSpan.class)) {
            MyCustomSpan s = (MyCustomSpan) span;

            // Get the range in spannableString where this span has been applied.
            // How do I do this?
        }
    }

Solution

  • Call getSpanStart() and getSpanEnd() to retrieve the start and end positions within the Spanned where the span is applied.