-1

Hello There I have to show an Progress Dialog in my async task, my async task is:

  private class UserProfileDialog extends AsyncTask<String, String, String> {

    private String imagePath;
    private String name;
    private String status;
    private String number;
    private Activity activity;
    private ProgressDialog progressDialog;

    public UserProfileDialog(Activity activity) {
        this.activity = activity;
    }

    @Override
    protected void onPreExecute() {
        progressDialog = new ProgressDialog(activity);
        progressDialog.setMessage("Please Wait...");
        progressDialog.setCancelable(false);
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        imagePath = params[0];
        name = params[1];
        status = params[2];
        number = params[3];
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    //show the dialog for dob date picker
                    final Dialog profileDialog = new Dialog(context);
                    profileDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                    profileDialog.setContentView(R.layout.profile_dialog_me);

                    TextView title = (TextView) profileDialog.findViewById(R.id.title);
                    title.setTypeface(EasyFonts.robotoBold(context));

                    ImageView imageCloser = (ImageView) profileDialog.findViewById(R.id.imageCloser);
                    final ImageView profilePicture_x = (ImageView) profileDialog.findViewById(R.id.profilePicture);

                    //set the profile picture
                    Bitmap originalBitmap = BitmapFactory.decodeFile(new File(imagePath).getAbsolutePath());
                    originalBitmap = ModifyOrientationUtility.modifyOrientation(originalBitmap, new File(imagePath).getAbsolutePath());
                    Bitmap bitmap = resizeBitmapFitXY(400, 400, originalBitmap);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    Glide.with(context)
                            .load(stream.toByteArray())
                            .asBitmap()
                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                            .skipMemoryCache(true)
                            .dontAnimate()
                            .into(new SimpleTarget<Bitmap>() {

                                @Override
                                public void onResourceReady(Bitmap arg0, GlideAnimation<? super Bitmap> arg1) {
                                    // TODO Auto-generated method stub
                                    profilePicture_x.setImageBitmap(arg0);
                                }
                            });

                    TextView profileName = (TextView) profileDialog.findViewById(R.id.profileName);
                    profileName.setTypeface(EasyFonts.robotoLight(context));
                    profileName.setText(name);

                    TextView profileNumber = (TextView) profileDialog.findViewById(R.id.profileNumber);
                    profileNumber.setTypeface(EasyFonts.robotoLight(context));
                    profileNumber.setText(number);

                    TextView profileStatus = (TextView) profileDialog.findViewById(R.id.profileStatus);
                    profileStatus.setTypeface(EasyFonts.robotoLight(context));
                    profileStatus.setText(status);
                    //UI Events
                    imageCloser.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            profileDialog.dismiss();
                        }
                    });
                    //show the dialog
                    profileDialog.show();
                } catch (Exception exp) {
                    MyMLogger.showMLog("xm", "show profile dialog " + exp.toString(), logFromClass, "showProfileDialog");
                    exp.printStackTrace();
                }
            }
        });
        return null;
    }

    @Override
    protected void onPostExecute(String s) {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        super.onPostExecute(s);
    }
}

The AsyncTask is working find but it do not shows me the progress dialog when I start my task on the button click!

But whenever I Dismiss the profile dialog it starts showing me the progress dialog (ignoring the onPostExecute).

Can somebody please figure the problem out, I am bit confusing why it is happening!

The Angel Cat
  • 59
  • 2
  • 10
  • 3
    Why are you using an `AsyncTask` there? You're running everything on the UI thread anyway. Also, why don't you let Glide handle the image resizing? – Mike M. Nov 03 '17 at 06:19
  • the problem is that I'm showing this in RecyclerView Adapter, and I am modifying the image orientation before setting in Glide! – The Angel Cat Nov 03 '17 at 06:21
  • I believe Glide can handle rotations, as well. – Mike M. Nov 03 '17 at 06:22

2 Answers2

1

Read ProgressDialog

super.onPreExecute(); should remove.

@Override
protected void onPreExecute() {
  // super.onPreExecute();
    progressDialog = new ProgressDialog(activity);
    progressDialog.setMessage("Please Wait...");
    progressDialog.setCancelable(false);
    progressDialog.show();

}

Problem

//UI Events
 imageCloser.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            profileDialog.dismiss();
                        }
                    });

A dialog showing a progress indicator and an optional text message or view. Only a text message or a view can be used at the same time.

Why are doing this in your doInBackground method?

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

try this,

private ProgressDialog pdia;

 @Override
 protected void onPreExecute(){ 
 super.onPreExecute();
    pdia = new ProgressDialog(yourContext);
    pdia.setMessage("Loading...");
    pdia.show();    
 }

  @Override
 protected void onPostExecute(String result){
 super.onPostExecute(result);
   if(pdia.isShownig){
    pdia.dismiss();
 }
 }

also check this link, progressDialog in AsyncTask

Jinal Awaiya
  • 441
  • 3
  • 14