Skip to content Skip to sidebar Skip to footer

Android Custom Progress Indicator While Doing Asynctask

When running an AsyncTask I want to display a progress indicator. This stock one is ugly so I made a layout with my own. There lies the problem however, it's a separate layout mean

Solution 1:

If you need more control over your ProgressBar, you can use WindowManager to add a view on top of everything. It can be done without any additional layout, activities or windows. You can control animation, touches, position and visibility just like in case of regular view. Fully working code:

finalProgressBarview=newProgressBar(TestActivity.this);
view.setBackgroundColor(0x7f000000);
finalLayoutParamswindowParams=newWindowManager.LayoutParams();
windowParams.gravity = Gravity.CENTER;
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;

newAsyncTask<Integer, Integer, Integer>() {
    publicvoidonPreExecute() {
        // init your dialog here;
        getWindowManager().addView(view, windowParams);
    }

    publicvoidonPostExecute(Integer result) {
        getWindowManager().removeView(view);
        // process result;
    }

    @Overrideprotected Integer doInBackground(Integer... arg0) {
        // do your things heretry {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        returnnull;
    }
}.execute();

Solution 2:

new AsyncTask<Params, Progress, Result>() {
   Dialog dlg = new Dialog();
   View customProgress;
   publicvoidonPreExecute() {
       //init your dialog here;
       View customProgress = LayoutInflater.from(CurrentActivity.this).inflate(R.layout.your_progress_layout, null, false);
       dialog.setContentView(view);
       dialog.show();
   }
   public Result doInBackground(Params... params) {
    // do something
    publishProgress(progress);  
   }
   publicvoidonProgressUpdate(Progress progress) {
    // do something with progressView;
   }
   publicvoidonPostExecute(Result result) {
      dlg.dissmiss();
      // process result;
   }
}.execute();

This is the possible way ( only sample, may contains errors)

Post a Comment for "Android Custom Progress Indicator While Doing Asynctask"