What is flag variable in building SpannableStringBuilder

394 views Asked by At

I read flags parameter in google docs, but I can't find what exactly it's doing. For example in below code:

SpannableStringBuilder builder1 = new SpannableStringBuilder();
builder1.append("hi");
builder1.append("this is test");
builder1.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
myTextView.setText(builder1);

There is no difference if you change 4th parameter and set variables like this: Spanned.SPAN_INCLUSIVE_INCLUSIVE,Spanned.SPAN_INCLUSIVE_EXCLUSIVE,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE.

There is no difference in result.

But when run this code:

SpannableStringBuilder builder1 = new SpannableStringBuilder();
builder1.append("hi");
builder1.append("this is test");
builder1.setSpan(new ForegroundColorSpan(Color.RED), 0, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

SpannableStringBuilder builder2 = new SpannableStringBuilder();
builder1.append("second test this is going to be different");
builder1.setSpan(new BackgroundColorSpan(Color.BLUE), 10, 15, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

SpannableStringBuilder all = new SpannableStringBuilder();
all.append(builder2);
all.insert(0, "\n");
all.insert(0, builder1);

You get different results based on 4th parameter that you pass to builder1.setSpan

Can somebody explains why this happens?

1

There are 1 answers

2
Deepali-Systematix On

The flag is set on spans that are being used to apply temporary styling information on the composing text of an input method, so that they can be found and removed when the composing text is being replaced