Reputation: 828
I need to update a custom progressBar in a custom layout which is being added to a ProgressDialog as a View.
Custom layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:background="@android:color/transparent" >
<com.android.library.circleprogress.ArcProgress
android:id="@+id/arc_progress"
android:background="@android:color/transparent"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_progress="5"
custom:arc_stroke_width="10dp"
android:layout_centerInParent="true"
custom:arc_bottom_text="Updating..."/>
</RelativeLayout>
Java:
public static ProgressDialog createProgressDialog(Context mContext, ViewGroup viewGroup) {
ProgressDialog dialog = new ProgressDialog(mContext);
try {
dialog.show();
} catch (BadTokenException e) {
}
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.progress_dialog, viewGroup);
// this is the progressBar I need to update
ArcProgress arcProgress = (ArcProgress) view.findViewById(R.id.arc_progress);
dialog.setCancelable(false);
dialog.setContentView(view);
return dialog;
}
And I am calling this create dialog in a doInBackground method from an AsyncTask, so I need to update the percentage number to make the bar moves.
...
protected void onPreExecute() {
super.onPreExecute();
pDialog = Util.createProgressDialog(context, null);
pDialog.show();
}
@Override
protected Integer doInBackground(String... params) {
String file = Util.getMapFiles().get(bean).toLowerCase() + ".txt";
// this download updates the pDialog.setProgress(percent)
Integer total = ftpDb.download(bean, Const.DIR_FTP_INICIO, file, pDialog);
return total;
}
Any ideas how I could achieve that? With default ProgressDialog I can update the percentage bar with no problem.
Upvotes: 0
Views: 790
Reputation: 442
Create an interface inside your AsyncTask, like this:
public interface AsyncTaskDelegate{
public void updateProgress(int progress);
}
Then in your AsyncTask constructor you want a reference to this delegate, like this:
private AsyncTaskDelegate mDelegate;
public MyAsyncTask(AsyncTaskDelegate del){
this.mDelegate=del;
}
After this, you can override the OnProgressUpdate, like this:
protected void onProgressUpdate(Integer... progress) {
this.mDelegate(progress[0]);
}
then in your doInBackground, whenever you want to update the progress you call the method publishUpdate(someinteger).
Remember to save a reference to your progressbar where you implement the delegate and use the progressbar.setProgress() there.
Upvotes: 1