tags:

views:

52

answers:

2

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?

+1  A: 
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();
BrennaSoft
That was quick! Thanks
OkyDokyman
A: 

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 http://stackoverflow.com/questions/3282940/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);
tomash