Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain the meaning of Span flags like SPAN_EXCLUSIVE_EXCLUSIVE

Tags:

android

Can someone clearly explain with examples what each of the span flags like SPAN_EXCLUSIVE_EXCLUSIVE and SPAN_MARK_MARK mean and when to use what flags?

I do not understand the official documentation when it says:

Spans of type SPAN_EXCLUSIVE_EXCLUSIVE do not expand to include text inserted at either their starting or ending point.

Does "expand to include" refer to edits made after inserting the spans?

Does it mean that these flags do NOT affect Spannables with immutable text?

like image 980
Dheeraj Vepakomma Avatar asked Mar 26 '12 20:03

Dheeraj Vepakomma


People also ask

What is Span_exclusive_exclusive?

SPAN_EXCLUSIVE_EXCLUSIVE. They are all the same! The flags don't affect the span. A span always includes the character at its start index and excludes the character at the end index.

What is a span in android?

Spans are powerful markup objects that you can use to style text at a character or paragraph level. By attaching spans to text objects, you can change text in a variety of ways, including adding color, making the text clickable, scaling the text size, and drawing text in a customized way.

What is spanned in java?

This is the interface for text that has markup objects attached to ranges of it. Not all text classes have mutable markup or text; see Spannable for mutable markup and Editable for mutable text.


1 Answers

What the flags don't mean

When I first saw the INCLUSIVE and EXCLUSIVE parts of the Spannable flags, I thought they just told whether or not the span included the start and end index positions of the span. This is not true. Let me illustrate that with the following example.

String myString = "01234"; int start = 1; int end = 3; int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing  SpannableString spannableString = new SpannableString(myString); ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(foregroundSpan, start, end, spanFlag); textView.setText(spannableString); 

Here are the results:

SPAN_INCLUSIVE_INCLUSIVE

enter image description here

SPAN_INCLUSIVE_EXCLUSIVE

enter image description here

SPAN_EXCLUSIVE_INCLUSIVE

enter image description here

SPAN_EXCLUSIVE_EXCLUSIVE

enter image description here

They are all the same! The flags don't affect the span. A span always includes the character at its start index and excludes the character at the end index.

What the flags really mean

The INCLUSIVE and EXCLUSIVE parts of the Spannable flags actually tell whether or not the span should include text that is inserted at the start or end positions.

Here is a modified example to illustrate that.

String myString = "01234"; int start = 1; int end = 3; int spanFlag = Spannable.SPAN_INCLUSIVE_INCLUSIVE; // this is what is changing  // set the span SpannableStringBuilder spannableString = new SpannableStringBuilder(myString); ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED); spannableString.setSpan(foregroundSpan, start, end, spanFlag);  // insert the text after the span has already been set // (inserting at start index second so that end index doesn't get messed up) spannableString.insert(end, "x"); spannableString.insert(start, "x");  textView.setText(spannableString); 

Here are the results after inserting an x at the end and start indexes:

SPAN_INCLUSIVE_INCLUSIVE

enter image description here

SPAN_INCLUSIVE_EXCLUSIVE

enter image description here

SPAN_EXCLUSIVE_INCLUSIVE

enter image description here

SPAN_EXCLUSIVE_EXCLUSIVE

enter image description here

Notes

  • In the second example I had to use a SpannableStringBuilder because the text in a SpannableString is immutable so you can't insert text into it. Thus, the flags are generally meaningless for a SpannableString. However, one could imagine a situation in which the spans from a SpannableString get copied over to a SpannableStringBuilder or Editable, and from there the flags would have meaning.
  • See this answer for the difference between SpannableString, SpannableStringBuilder, Editable, and more.
like image 198
Suragch Avatar answered Sep 19 '22 04:09

Suragch