Home » Java » Finding EditText Editable getSpans Start and End Index

Finding EditText Editable getSpans Start and End Index

Posted by: admin December 28, 2021 Leave a comment


I am trying to get All Spans applied to text as below;

public String getTextWithTags(Editable e)

    StyleSpan[] ss = e.getSpans(0,e.length(),StyleSpan.class);
    ss[0].getSpanStart <--- ? This is the problem, no such function

    return "";

But there is no index find function to replace tags to store them on database so i can retrieve all spans back when i reopen text. How can i get all span positions from editable object?

StyleSpan[] ss = e.getSpans(0,e.length(),StyleSpan.class);

for(StyleSpan span : ss){
    int start = e.getSpanStart(span);
    int end = e.getSpanEnd(span);


If u want to find Span start/end index, you can use this(example for a single RelativeSizeSpan) –

val sb = text as SpannableStringBuilder
val firstSpan = getSpans(0, length, RelativeSizeSpan::class.java).first()
val start = (sb as Spanned).getSpanStart(firstSpan)
val end = (sb as Spanned).getSpanEnd(firstSpan)