Reputation: 2311
I have a TextView and I want each second to highlight another letter in the word.
For example: h e l l o - h e l l o - h e l l o - h e l l o - h e l l o
What I have done:
int i = 0;
String text;
Handler handler = new Handler();
public void spanText(String txt) {
text = txt;
for(int i=0; i<text.length(); i++) {
handler.post(runnable);
}
Runnable runnable = new Runnable() {
@Override
public void run() {
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
StyleSpan style = new StyleSpan(Typeface.BOLD);
spannable.setSpan(style, i, i+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
bestResult.setText(spannable, BufferType.SPANNABLE);
i++;
if(i < text.length())
handler.postDelayed(runnable, 5000);
}
};
Unfortunately it doesn't work and I can see only the last letter highlighted.
Thank you in advance.
Upvotes: 4
Views: 3944
Reputation: 54672
int i = 0;
String text;
Handler handler = new Handler();
public void spanText(String txt) {
text = txt;
handler.post(runnable);
Runnable runnable = new Runnable() {
@Override
public void run() {
Spannable spannable = Spannable.Factory.getInstance().newSpannable(text);
StyleSpan style = new StyleSpan(Typeface.BOLD);
spannable.setSpan(style, i, i+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
bestResult.setText(spannable, BufferType.SPANNABLE);
i++;
if(i < text.length())
handler.postDelayed(runnable, 5000);
}
};
Upvotes: 3