Shreyash Mahajan
Shreyash Mahajan

Reputation: 23596

How to implement the Progressdialog here?

I am saving the imae from drawing canvas. It takes some seconds to geting save to the sdcard. So, During the process of save i want to show the dialog that the Image getting save...

So how to implement the progress dialog for that ?

The code for Image save is as like below:

 case R.id.saveBtn:
            Toast.makeText(getApplicationContext(), "Save", Toast.LENGTH_SHORT).show();
            final Activity currentActivity  = this;
            Handler saveHandler = new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    final AlertDialog alertDialog = new AlertDialog.Builder(currentActivity).create();
                    alertDialog.setTitle("Drawing App");
                    alertDialog.setMessage("Your drawing had been saved :)");
                    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
                    alertDialog.show();
                }
            } ;

           new ExportBitmapToFile(this,saveHandler, drawingSurface.getBitmap()).execute();

        break;

Upvotes: 0

Views: 270

Answers (3)

Vladimir
Vladimir

Reputation: 9753

Since you are using AsyncTask (ExportBitmapToFile) you can call ProgressDialog in preExecute() and dismiss it in postExecute method. There are no Handlers required to do this.

edit

class ExportBitmapToFile extends AsyncTask<...> {
    private ProgressDialog  m_progressDialog = null;
    .... 

    @Override
    protected void onPreExecute(){
        m_progressDialog = new ProgressDialog(m_context);
        m_progressDialog.setMessage("please wait...");
        m_progressDialog.setCancelable(false);
        m_progressDialog.show();
    }

    @Override
    protected void onPostExecute(HashMap<String, String> result){
        m_progressDialog.dismiss();
        //your alert dialog with message to user
        AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
            builder.setTitle("Drawing App");
            builder.setMessage("Your drawing had been saved :)");
            builder.setButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    return;
                }
            });
        alertDialog = builder.create()
        alertDialog.show();
    } 
}

Upvotes: 2

Serdar Dogruyol
Serdar Dogruyol

Reputation: 5157

You should use onPreExecute and onPostExecute in your ExportBitmapToFile AsyncTask Class so you should add something like that to that class.

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();

        }
        super.onPostExecute(result);
    }



    @Override
    protected void onPreExecute() {

        progressDialog = ProgressDialog.show(context, "Message",
                "Loading");

        super.onPreExecute();

    }

Upvotes: 1

FunkTheMonk
FunkTheMonk

Reputation: 10938

It looks like ExportBitmapToFile is an AsyncTask, in which case call saveHandler.sendEmptyMessage(0); in onPreExecute().

You could also call savehandler.sendEmptyMessage(1) in onPostExecute() and make the alertDialog a member variable (rather than local instance) of you activity and look at msg.what in your handleMessage(Message msg); function - a 0 means show the dialog, a 1 means the dialog should be hidden. Also, you might want to clean up the dialog in the activities onStop

You also want to set all the properties on the AlertDialog.Builder before calling create():

private AlertDialog alertDialog = null; 
    Handler saveHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 0 && alertDialog != null) {
                AlertDialog.Builder builder = new AlertDialog.Builder(currentActivity);
                builder.setTitle("Drawing App");
                builder.setMessage("Your drawing had been saved :)");
                builder.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        return;
                    }
                });
                alertDialog = builder.create()
                alertDialog.show();
            }
            else if(alertDialog != null){
                alertDialog.dismiss();
                alertDialog = null;
            }
        }
    } ;

Note - this doesn't show a progress indicator - you should display a ProgressDialog onPreExecute, dismiss it and then show the above AlertDialog on PostExecute (using the same technique).

Upvotes: 1

Related Questions