0

I use BackgroundColorSpan to show multiple highlights for a TextView. It shows correctly on Android API 23 and below, but has a problem with API 24 and 25.

final static String article1 = "Last year’s Google Pixel and Pixel XL were without question " +
        "two of the hottest new smartphones of 2016. Google surprised fans by ditching its " +
        "Nexus lineup of devices and moving from affordable upper mid-range devices to " +
        "high-end flagship phones. Sure they were more expensive than Google phones had " +
        "been in the past, but they were also a much better platform for showcasing the best " +
        "of what Android has to offer, featuring high-end specs and a sleek metal and " +
        "glass design.";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SpannableStringBuilder sb = new SpannableStringBuilder(article1);

    BackgroundColorSpan span;
    for (int i = 0; i < 3; i++) {
        span = new BackgroundColorSpan(Color.YELLOW);
        sb.setSpan(span, 23, 214, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    span = new BackgroundColorSpan(Color.RED);
    sb.setSpan(span, 67, 83, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    span = new BackgroundColorSpan(Color.RED);
    sb.setSpan(span, 216, 236, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

    TextView tv = (TextView) findViewById(R.id.body);
    tv.setText(sb);
}

}

If loop 3, only one red highlight will show. If loop 2, both red highlights will show.

Eric
  • 1
  • 1

1 Answers1

0

It seems related to some changes in API 24+

  1. https://issuetracker.google.com/issues/37085040
  2. https://issuetracker.google.com/issues/37135960
Eric
  • 1
  • 1