I'm trying to set a large text in a EditText, the operation can take over 30 seconds so I use a ProgressDialog. It shows up, but there's no animation, and then disappears when the operation is done. Here's my simplified code:
class FileOpener extends AsyncTask<File, Integer, String> {
private ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(context);
...
progress.show();
}
@Override
protected StringBuilder doInBackground(File... files) {
return readFile();
}
@Override
protected void onPostExecute(String content) {
EditText editText = ...
editText.post(new Runnable() {
@Override
public void run() {
editText.setText(content);
progress.dismiss();
}
});
}
}
What can I do to animate the progress dialog while setting text?
I also tried using this in onPostExecute
, same thing, dialog is there but no animation...
EditText editText = ...
new Thread() {
@Override
public void run() {
editor.setText(content);
progress.dismiss();
}
}.start();
EDIT - This is NOT a question on my EditText speed, which is awful as I understood. This question is here. No matter how I improve the speed, setting the text will ALWAYS take a few seconds with big files, it doesn't even with top editing apps. What this question is really about is how to keep the loading dialog animation, because currently, it doesn't animate while setting the text in the EditText. I know nothing nothing can be changed on the UI if not in UI thread, then how can I update/animate the loading? If this is impossible or simply too complicated or hacky, then how can I show a loading animation of any sort while setting the text.
You should be able to minimize the animation pausing/stopping by creating an EditText and setting its text in your
doInBackground
and adding it to view in youronPostExecute
(the ui thread). Here is an example using an Activity and variation of the task you already have with some code comments:I tested the above using a http://www.webpagefx.com/tools/lorem-ipsum-generator/loremipsum/paragraphs Paragraphs: 100 Size: 46929 bytes
Set to one string value
fiftykb
and then created another Stringall
very lazily and set it to the new edit text (instead of yourreadFile()
):Results were that the animation never stops, but there is a brief moment (< 1 sec) the edit text is blank and then shows all the text and is editable.
HTHs!