Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How do you efficiently load a large amount of text in a TextView?

I'm writing an Android app that reads a single text file and display it on a TextView.

What I'm doing right now is read the whole file into a String (using BufferedReader and StringBuilder) and display it on a TextView using setText(string). A 700KB text file can take about 2 to 3 seconds before it is being displayed on the screen.

But I've used some other ebook readers on the market and they can display the same text almost instantly. Anyone know how I can achieve this?

Thanks you.

Edit: Many suggest ListView, but it doesn't work for my particular case. This is from my reply to one of the answer: ...[ListView] doesn't work for me for a few reasons. (1) To make the Listview look like a TextView, we have to break the text up on new line character. If I load a single large paragraph, it's just as slow as a loading a TextView. (2) Since a ListView only measures the item on the screen, I cannot know ahead of time the total 'pages' or 'height' of the entire text.

like image 213
Ray Zhou Avatar asked Jan 29 '13 16:01

Ray Zhou


People also ask

Which method is used to set the text in a TextView?

Set The Text of The TextView You can set the text to be displayed in the TextView either when declaring it in your layout file, or by using its setText() method.

How do I limit text in TextView?

To set the maximum length for text in TextView widget, set the maxLength attribute with required number of length. If the length of text exceeds the maximum length, then the text is truncated to the specified length.

How do I Auto Resize TextView?

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes: Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.


2 Answers

Essentially, the key is to only load the data that you need, when you need it. One way to do this would be to put every paragraph into it's own TextView, which is put into a ListAdapter, which is included into a ListView. There has to be some kind of an index set in place, such that each paragraph knows where in the data file to find. This interface will allow you to load only what you need, when you need it. Your list adapter looks something like this (This code isn't complete, but it should give you an idea at least of what you should do):

class ParagraphAdapter extends ListAdapter{
ArrayList<Integer> mLocations; // Somewhere define this to your locations, I'll leave that for you to figure out
protected View getView(int position,View convertView, ViewGroup parent)
{
  mLocations.get(position); // Read the file starting at this position, until the next value
  String text; // This is the output of the above
  TextView tv=new TextView(context);
  tv.setText(parent.getContext());
}
}

It can be noted that Amazon uses a system of paging for the Kindle App. If you have the app, you can see at the bottom of each page what section you are on. Each "page" is probably closer to a sentence or so in length. Then it's just a matter of getting the right page, which can fairly quickly be done.

like image 166
PearsonArtPhoto Avatar answered Oct 21 '22 19:10

PearsonArtPhoto


To add on what @PearsonArtPhoto has said -

I suggest you implement some sort of paging mechanism, to divide your text into pages.
What you should do is split your text according to let's say N +M number of characters per pages.
N = fixed number of characters.
M = number of characters from N to nearest end of line character (so you won't see the last line being "cut").
I would suggest that if your android device allows you to hold this "in memory" -
do that,
and don't try to fetch this from the file one page after the other, but rather fetch from the "in memory" structure - this will improve performance.
Once you scroll and realize you need to fetch the next page, fetch it from the "in memory" structure.

like image 36
Yair Zaslavsky Avatar answered Oct 21 '22 20:10

Yair Zaslavsky