Research Development
Research Development

Reputation: 904

how to use Custom Progress bar in Asynk task in android

public class LoginActivity extends Activity {

    public static final int DIALOG_LOADING = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        showDialog(DIALOG_LOADING);
        Thread thread =  new Thread(null, doSomeTask);
        thread.start();

    }


    private Runnable doSomeTask = new Runnable() {
        public void run() {
            try {
                //Code of your task
                Thread.sleep(10000);
            } catch (InterruptedException e) {}
            //Done! now continue on the UI thread
            runOnUiThread(taskDone);
        }
    };

    //Since we can't update our UI from a thread this Runnable takes care of that!
    private Runnable taskDone = new Runnable() {
        public void run() {
            dismissDialog(DIALOG_LOADING);
        }
    };



@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_LOADING:
                final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                //here we set layout of progress dialog
                dialog.setContentView(R.layout.custom_progress_dialog);
                dialog.setCancelable(true);
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub

                    }
                });
                return dialog;

            default:
                return null;
        }
    };

Using Upper code i am able to show Custom Progress bar but it set on thread with Specified time but i have asynk task below .

   class download extends AsyncTask<Void,Void,Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
    }
}

i want to set Here Progress bar once do in background finish then progress bar should disappear.please suggest me

Upvotes: 1

Views: 130

Answers (5)

Android Leo
Android Leo

Reputation: 676

Using AsyncTask, you can show your dialog in onPreExecute() and after downloaded your data in onPostExecute() you can dismiss it. Start your download task into doInBackground().

public class Download extends AsyncTask<Void,Void,Void>{
    private Activity activity;
    private static final int DIALOG_LOADING = 1;
    public Download(Activity activity){
         this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // before start download task
        activity.showDialog(DIALOG_LOADING);
    }

    @Override
    protected Void doInBackground(Void... voids) {
         // start dowanload task here
         return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        // after downloaded data
        activity.dismissDialog(DIALOG_LOADING);
    }
}

Upvotes: 1

Salauddin Gazi
Salauddin Gazi

Reputation: 1517

public class MyAsyncTask extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;

public SavePatientData(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {

    super.onPreExecute();
    pDialog = null;
    pDialog = new ProgressDialog(context, ProgressDialog.THEME_HOLO_LIGHT);
    pDialog.setMax(100);
    pDialog.setCancelable(false);
    pDialog.setMessage("Saving\n .....");
    pDialog.setContentView(// your coustom layout);
    pDialog.show();
}

@Override
protected String doInBackground(String... params) {
    // do someting
    return null;
}

@Override
protected void onProgressUpdate(String... values) {
    super.onProgressUpdate(values);
    pDialog.setProgress(Integer.parseInt(values[0]));
}

@Override
protected void onPostExecute(String result) {
    if ((pDialog != null) && (pDialog.isShowing())) {
    pDialog.dismiss();
}

}

Upvotes: 0

Damodhar
Damodhar

Reputation: 1317

Hi there i just modify

public class LoginActivity extends Activity {

    public static final int DIALOG_LOADING = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);


        new download().execute();

    }


   class download extends AsyncTask<Void,Void,Void>
{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
       showDialog(DIALOG_LOADING);
    }

    @Override
    protected Void doInBackground(Void... voids) {
        //do your stuff here 
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        dismissDialog(DIALOG_LOADING);
    }
}


@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_LOADING:
                final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                //here we set layout of progress dialog
                dialog.setContentView(R.layout.custom_progress_dialog);
                dialog.setCancelable(true);
                dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        // TODO Auto-generated method stub

                    }
                });
                return dialog;

            default:
                return null;
        }
    };

Upvotes: 0

Sasi Kumar
Sasi Kumar

Reputation: 13313

Runnable r = new Runnable()
{
@Override
public void run()
{
    // your code here
}
};

 Thread t = new Thread(r);
 t.start();

AsyncTask task

class download extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(DIALOG_LOADING); //custom alert start when AsyncTask start

}

@Override
protected Void doInBackground(Void... voids) {
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
  dismissDialog(DIALOG_LOADING);  //custom dialog  Dissmiss when AsyncTask completed

}
}

Upvotes: 0

Ajin kumar
Ajin kumar

Reputation: 311

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(TabsActivity.this);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setIndeterminate(false);
        dialog.setMessage("Loading ...");
        dialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {        
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
            dialog.dismiss();              
    }

Upvotes: 2

Related Questions