I want to change color of parts of a text several times with a timer.
Simplest way is this:
SpannableStringBuilder ssb = new SpannableStringBuilder(mainText);
ForegroundColorSpan span = new ForegroundColorSpan(Color.BLUE);
ssb.setSpan(span, start, end, 0);
tv.setText(ssb);
But if I run the above code several times in a second, I actually change the whole (large) text of TextView
each time so a unwanted memory-CPU load will happen specifically on lower-end devices.
How can I have a single Span
on TextView
and only change the Span
start and end position?
Will it work at all or a full text replace will happen behind the scene?
My text is fixed and won't change never.
execute below code once:
and every time you want to change span do this:
Solution for span movement without calling
setText
method:Solution for dynamic color change:
and testing code:
I meet the same issue this afternoon,and here is my solution:
I found it's a much more efficient way,in my project I used set-whole-text method took 400~600 milliseconds,by this way only 0 or 1 millisecond.