How to loop through the spans in a SpannedString or SpannableString in Android -


if have spannedstring (or spannablestring) like this

enter image description here

spannablestring spannablestring = new spannablestring("hello world!"); foregroundcolorspan foregroundspan = new foregroundcolorspan(color.red); backgroundcolorspan backgroundspan = new backgroundcolorspan(color.yellow); spannablestring.setspan(foregroundspan, 1, 8, spannable.span_exclusive_exclusive); spannablestring.setspan(backgroundspan, 3, spannablestring.length() - 1, spannable.span_exclusive_exclusive); textview.settext(spannablestring); 

how loop through spans of resulting string?

(i'm posting self answer since have been learning how recently.)

looping through spans in order

you can use getspans array of spans in spanned or spannable string. however, looping through getspans results not give them in order. them in order can use nextspantransition.

here example spannedstring example in question. (a spannablestring work same.) green lines show span transitions are. text black default.

enter image description here

the code finds next span transition , gets spans in current range.

int next; (int = 0; < spannablestring.length(); = next) {      // find next span transition     next = spannablestring.nextspantransition(i, spannedstring.length(), characterstyle.class);      // spans in range     int numofspans = 0;     characterstyle[] spans = spannablestring.getspans(i, next, characterstyle.class);     for(int j = 0; j < spans.length; j++) {         numofspans++;     }      log.i("tag", "spans " + + " " + next + ": " + numofspans); } 

output:

spans 0 1: 0 spans 1 3: 1 spans 3 8: 2 spans 8 11: 1 spans 11 12: 0 

thanks this code ideas.

types of spans

normally when looping through spans choose type of span. example, if wanted remove foreground color spans, following:

// spans foregroundcolorspan[] spans = spannablestring.getspans(0, spannablestring.length(), foregroundcolorspan.class);  // loop through spans (foregroundcolorspan span : spans) {     spannablestring.removespan(span); } 

note wouldn't work spannedstring because spans in spannedstring not mutable (see this answer).

if wanted spans of type set type object.class.

object[] spans = spannablestring.getspans(0, spannablestring.length(), object.class); 

if wanted spans affect appearance @ character level, use characterstyle.class. if within loop wanted further limit spans belonging metricaffectingspan, this.

characterstyle[] spans = spannablestring.getspans(0, spannablestring.length(), characterstyle.class); (characterstyle span : spans) {     if (span instanceof metricaffectingspan) {         //     } } 

here general hierarchical breakdown of span types. may not complete. read spans, powerful concept more information.

object     characterstyle         backgroundcolorspan         clickablespan             urlspan         foregroundcolorspan         maskfilterspan         strikethroughspan         suggestionspan         underlinespan          metricaffectingspan             absolutesizespan             localespan             relativesizespan             replacementspan                 dynamicdrawablespan                     imagespan              scalexspan             stylespan             subscriptspan             superscriptspan             textappearancespan             typefacespan      paragraphstyle         alignmentspan             alignmentspan.standard         bulletspan         drawablemarginspan         iconmarginspan         leadingmarginspan             leadingmarginspan.leadingmarginspan2             leadingmarginspan.standard         linebackgroundspan         lineheightspan             lineheightspan.withdensity         quotespan         tabstopspan             tabstopspan.standard         wraptogetherspan 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -