0
votes

i m newcomer in android. i am faceing some trouble my Scenarios is -when i click button so open alertDailog and alertdailog have two button like send and cancel,when i click send button, i wanna to open a ProgressBar because send button have havy content, so take more time.

i m using handler but not found any exact solution

i m using this code

btn.setOnClickListener(new View.OnClickListener() { public void onClick(View v) {

                handler.sendEmptyMessage(0);

        }

    });
  alert = new Dialog(ProgramDetailActivity.this);

        alert.setContentView(R.layout.dialog_email);
        alert.setTitle("                   Enter mail info");
        alert.setCancelable(true);
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,
                    int id) {
                finish();
            }
        };
             btnSend = (Button) alert.findViewById(R.id.btnsend);
         btnBack = (Button) alert.findViewById(R.id.btncancel);

        alert.show();

btnSend.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("null")
                         public void onClick(View v) {
                        handler2.sendEmptyMessage(0);

// perform long opration progressDialog.dismiss();

                         toast=Toast.makeText(ProgramDetailActivity.this,"Mail has been sent Sucessfully!",

Toast.LENGTH_LONG); toast.show();

} }

private Handler handler2 = new Handler() { public void handleMessage(Message msg) { alert.hide(); alert.cancel(); alert.dismiss();

progressDialog = ProgressDialog.show(v.getContext(),                                "Email Sending", "Please wait...");
    }
};
1
when click button ,i passed the request to handler.then handler open the alert dailog. - Rajesh Sharma

1 Answers

1
votes

I have also used Progressdialog in click event of Alertdialog.The code is below:

AlertDialog.Builder submitalert = new AlertDialog.Builder(Summary.this);
 submitalert.setTitle(getResources().getString(R.string.app_name))
.setMessage("Submit the data to database?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener(){

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    update.setTitle(getResources().getString(R.string.app_name));
                    update.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    update.setCancelable(true);
                    update.setMax(100);
                    update.show();


                    Thread background = new Thread (new Runnable() {
                           public void run() {
                               try {
                                   // enter the code to be run while displaying the progressbar.
                                   //
                                   // This example is just going to increment the progress bar:
                                   // So keep running until the progress value reaches maximum value
                                   while (update.getProgress()<= update.getMax()) {
                                       // wait 500ms between each update
                                       Thread.sleep(500);

                                       // active the update handler
                                       progressHandler.sendMessage(progressHandler.obtainMessage());
                                   }

                               } catch (java.lang.InterruptedException e) {
                                   // if something fails do something smart
                               }
                           }
                        });

                        // start the background thread
                        background.start();
                        if(update.getProgress()== 100)
                           {
                               update.dismiss();
                           }
                    }




            })
            .setNegativeButton("No", new DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            })
            .show();