I have an activity that's supposed to display a text file. The text file is 165 kB. Originally, after I read in the text file, I would do something like this,
textView.setText(fileText);
But that took over 10 seconds on my Nexus 4, and I confirmed that this was due to the call to setText()
and not reading the file. Then, after searching around a bit, I got the idea to put it in a webview, using WebView.loadData()
. This takes less than a second.
I'm curious as to why a WebView is able to load text so much faster than a TextView. Does anyone know?
From what I remember;
The WebView will only render the text that it needs to display at any given time. Meaning that when you first load it will only load 20 - 30 lines of text. Whereas when you call textView.setText(fileText);
it causes Android to render all of the text even though it is not displayed. If you are trying to add a lot of text to a TextView then you should use textView.append(fileLine);
in a loop which should load quite a bit faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With