Reputation: 3866
What do I do if tv.setText(Html.fromHtml(text));
takes too long, and hangs the UI?
If I can do it with a thread, can you provide an example?
Upvotes: 4
Views: 3370
Reputation: 12962
If you don't need to parse long or complex HTML, manual composing of Spannable
is much faster than using Html.fromHtml()
. Following sample comes from Set color of TextView span in Android
TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(wordtoSpan);
Upvotes: 3
Reputation: 73484
private Handler mHandler = new Handler() {
void handleMessage(Message msg) {
switch(msg.what) {
case UPDATE_TEXT_VIEW:
tv.setText(msg.obj); // set text with Message data
break;
}
}
}
Thread t = new Thread(new Runnable() {
// use handler to send message to run on UI thread.
mHandler.sendMessage(mHandler.obtainMessage(UPDATE_TEXT_VIEW, Html.fromHtml(text));
});
t.start();
Upvotes: 7