5

Is there a workaround for how long a TextView takes to set its text? I am trying to set a very long string and it ends up blocking the UI thread for a good 2-3 seconds because of that. Since I can't access the TextView from a different thread, I'm completely stumped.

Edit: Currently, I build up the string inside an AsyncTask doInBackground(), and only call TextView.setText() within the onPostExecute(which is running on the uiThread) The TextView is placed within a ScrollView

jkau
  • 465
  • 4
  • 12

2 Answers2

3

You shouldn't have a problem loading even a couple of screens worth of information. If the string is super long, you might consider lazy loading the content in sections.

Steven Mann
  • 558
  • 3
  • 14
  • Would that require using a ListView with each line being a separate TextView? – jkau Jun 24 '13 at 17:49
  • 2
    No, split the string in decent sized equal parts into an array. You would then just keep track of the scroll position in the textview: when it is near the bottom, you load more text into the view using append(). So textview.append(array.get(position));. – Steven Mann Jun 24 '13 at 18:05
  • Interesting that makes sense, would I have to make my own custom textview then? – jkau Jun 24 '13 at 18:16
  • No. You can read about the append method [here](http://developer.android.com/reference/android/widget/TextView.html#append%28java.lang.CharSequence%29) and use your choice of arrays. I would go with [ArrayList](http://developer.android.com/reference/java/util/ArrayList.html). Then just write the connection logic, which is really just storing the position variable and incrementing it with every new load. – Steven Mann Jun 24 '13 at 18:22
  • I understand the append method and the whole idea of storing into an array. Where exactly would I be putting the logic for storing the position and appending to the TextView though? – jkau Jun 24 '13 at 18:28
  • I think your best bet would be to wrap the TextView in a Scrollview, and then put the code in a listener like [this](http://stackoverflow.com/questions/8181828/android-detect-when-scrollview-stops-scrolling). I wish that you could attach a listener onto the TextView directly, but life goes on... – Steven Mann Jun 24 '13 at 18:44
  • Even though it ended up making the scrolling a bit wonky the first time around, it's probably the best option available. Thanks! – jkau Jun 24 '13 at 23:27
  • Is there a third-party text view control that can display large text? This suggested method would not give correct scroll bar position to users until the user scrolls to the end of the document. – Damn Vegetables Mar 12 '16 at 14:25
0

Since I can't access the TextView from a different thread,

You can use a different Thread and still access your TextView. Without knowing your code its hard to give a good example but you can use runOnUiThread

or

you could use an AsyncTask. Doing it this way, you can do whatever you need to in doInBackground() and set the text in onPostExecute()

Although, I'm not sure how long your string is to take several seconds to load. If you are doing a lot of work to get the string put together then I would suggest using AsyncTask

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I am already currently using an AsyncTask. The only thing I do in onPostExecute() is set the text in the TextView, and it is blocking the thread. So basically it would have the same result if I tried runOnUiThread anyways – jkau Jun 24 '13 at 17:47
  • Ok, this is the kind of information that should be in your OP so we can help better. Maybe post how you are doing it in your `AsyncTask` so we can help find a better way – codeMagic Jun 24 '13 at 17:49
  • @codeMagic runOnUIThread equals continue working on uiThread. So you dont access to textview from diffrent thread unless switch to ui thread. – Talha Mar 02 '15 at 16:16
  • @Talha you can access it's getters from a background `Thread` but you can't update a `View` from the background. – codeMagic Mar 02 '15 at 16:20
  • @codeMagic you are right, i wanted to say this also. – Talha Mar 02 '15 at 16:21
  • @codeMagic what matters is setText() that blocks UI Thread and can't do it in doInBackground() – David Mar 02 '16 at 08:11