I want to show a ProgressDialog while programatically it's adding multiples views to a layout. I have declared a ProgressDialog at the main Activity and then an AsyncTask is started that add the multiples views. On the onPostExecute of the AsyncTask the ProgressDialog is dismissed. The application works, but the problem is that the start up is slow and the ProgressDialog briefly freeze. This is the main cycle of the AsyncTask. Column extend a LinearLayout.
for(int i=0;i<7;i++){
((LinearLayout) Global.a.findViewById(R.id.scrollLayout)).addView(new Column(Global.a,i));
}
Just based on your code snippet, it appears you are using the AsyncTask incorrectly.
for(int i=0;i<7;i++){
((LinearLayout) Global.a.findViewById(R.id.scrollLayout)).addView(new Column(Global.a,i));
}
This should never be done in an AysncTask. If its done it doInBackground then you are modifying the UI from a different thread, if its done anywhere else (onPreExecute(), onPostExecute() or onProgressUpdate() then you are running a for loop in the UI thread.
The best solution would be to create an xml file with the views you want to add to it already in it. Though if you are intent on adding views in an AsyncTask then you need to change how its done.
private SomeName extends AsyncTask<Void,Column,Void>{
private Linearlayout layout;
protected void onPreExecute(Void... unused){
//UI thread
//show dialog
layout = (LinearLayout) Global.a.findViewById(R.id.scrollLayout);
}
protected void doInBackground(Void... unused){
//Background thread
for(int i=0;i<7;i++){
//create the view in the background
Column column = new Column(Global.a,i);
//call publish progress to update the UI
publishProgress(column);
}
}
protected void onProgressUpdate(Column... column){
//UI thread
//Update the UI
layout.addView(column[0]);
}
protected void onPreExecute(Void... unused){
//UI thread
//dismiss dialog
}
}
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