I have an AsyncTask which is taken a context:
public class DownloadDataPromotions extends AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>>
{
Context context;
public DownloadDataPromotions(Context context)
{
this.context = context;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
}
@Override
protected ArrayList<HashMap<String, String>> doInBackground(Void... params)
{
ArrayList<HashMap<String, String>> promoList = new ArrayList<HashMap<String, String>>();
promoList = DownloadingDataFromWebService();
...
return promoList;
}
@Override
protected void onProgressUpdate(Integer... values)
{
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> promoList)
{
super.onPostExecute(promoList);
...
}
}
Then I'm calling it onto a ListActivity(with his context) in order to display all content downloaded from this AsyncTask into a List.
But now I want to add in this AsyncTask a Loader ProgressBar which is only determinate by: displaying on the onPreExecute() and stop displaying in the onPostExecute() methods..
I have this into drawable content with a custom drawable but I want to make it appear when AsyncTask is loading and disappear when AsyncTask finished (programmatically)..
Edit:
This is working fine:
ProgressBar pb = new ProgressBar(TabPromotionsJSONParsingActivity.this);
LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayoutProgressBar);
ll.addView(pb);
pb.setVisibility(View.VISIBLE);
But the problem I have now is to stop it when my Asynctask finishes.. I would like to do the same task into my DownloadDataPromotions, someone knows how?
Add the code to create the ProgressBar in the onPreExecute method and add code to hide it in the onPostExecute method. Something like below (code to be added to your class) :
public class DownloadDataPromotions extends AsyncTask<Void, Integer, ArrayList<HashMap<String, String>>>
{
ProgressBar pb;
...
@Override
protected void onPreExecute()
{
super.onPreExecute();
pb = new ProgressBar(context);
LinearLayout ll = (LinearLayout) context.findViewById(R.id.linearlayoutProgressBar);
ll.addView(pb);
pb.setVisibility(View.VISIBLE);
}
...
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> promoList)
{
super.onPostExecute(promoList);
if (pb!=null) {
((LinearLayout)pb.getParent()).removeView(pb);
}
}
}