ProgressDialog animation pauses on big setText ()

I am trying to set a large text in an EditText, the operation may take more than 30 seconds, so I use ProgressDialog. It appears, but no animation, and then disappears when the operation is performed. Here is 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 execution dialog when setting up the text?

I also tried using this in onPostExecute , the same thing, there is a dialogue, but there is no animation ...

 EditText editText = ... new Thread() { @Override public void run() { editor.setText(content); progress.dismiss(); } }.start(); 

EDIT . This is not a question of the speed of my editing, which is terrible, as I understand it. This question is here . No matter how I improve the speed, installing the text will ALWAYS take a few seconds with large files, even with editing applications. Actually, this question is how to save the animation of the download dialog, since it is not currently animating when setting text in EditText. I don’t know anything, nothing can be changed in the user interface, if not in the user interface thread, then how can I update / revive the download? If this is not possible or just too complicated or hacked, then how can I show any type of loading animation when setting up the text.

+6
source share
1 answer

You should be able to minimize the pause / stop of the animation by creating an EditText and setting your text to doInBackground and adding it for viewing in your onPostExecute (stream ui). Here is an example of using Activity and a variation of the task that you already have with some code comments:

 public class Activity extends AppCompatActivity { /** * dummy edit text (or the old one if doing an update) */ private EditText dummyEditTextForShow; /** * that dummy parent */ private ViewGroup viewGroupToAddEditTextTo; /** * that dummy position in it parent */ int indexOfDummy; /** * your progress dialog */ private ProgressDialog progress; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // create the things progress = new ProgressDialog(this); dummyEditTextForShow = new EditText(this); // show the place holder setContentView(dummyEditTextForShow); // get its parent and index in parent viewGroupToAddEditTextTo = (ViewGroup) dummyEditTextForShow.getParent(); indexOfDummy = viewGroupToAddEditTextTo.indexOfChild(dummyEditTextForShow); new FileOpener().execute(); } class FileOpener extends AsyncTask<File, Integer, EditText> { @Override protected void onPreExecute() { progress.show(); } @Override protected EditText doInBackground(File... files) { EditText newEditText = new EditText(Activity.this); newEditText.setText(readFile()); return newEditText; } @Override protected void onPostExecute(EditText editText) { //get rid of old for the new ViewGroup.LayoutParams oldLayoutParams = dummyEditTextForShow.getLayoutParams(); viewGroupToAddEditTextTo.removeView(dummyEditTextForShow); viewGroupToAddEditTextTo.addView(editText, indexOfDummy, oldLayoutParams)); progress.hide(); dummyEditTextForShow = editText } } } 

I tested above using http://www.webpagefx.com/tools/lorem-ipsum-generator/loremipsum/paragraphs Points: 100 Size: 46929 bytes.

Set one string value to fiftykb , and then create another String all very lazy and set it to the new editing text (instead of readFile() ):

 String all = fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb + fiftykb; newEditText.setText(all) 

The results were that the animation never stops, but there is a short (<1 s) edit text is empty and then displays all the text and is edited.

HTHS!

+5
source

Source: https://habr.com/ru/post/1014287/


All Articles