If you want to show a progress bar, try the following steps, and copy and paste all the code into the appropriate part of your code, and it should work.
//the first thing you need to to is to initialize the progressDialog Class like this final ProgressDialog progressBarDialog= new ProgressDialog(this); //set the icon, title and progress style.. progressBarDialog.setIcon(R.drawable.ic_launcher); progressBarDialog.setTitle("Showing progress..."); progressBarDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //setting the OK Button progressBarDialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton){ Toast.makeText(getBaseContext(), "OK clicked!", Toast.LENGTH_SHORT).show(); } }); //set the Cancel button progressBarDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int whichButton){ Toast.makeText(getApplicationContext(), "Cancel clicked", Toast.LENGTH_SHORT).show(); } }); //initialize the dialog.. progressBarDialog.setProgress(0); //setup a thread for long running processes new Thread(new Runnable(){ public void run(){ for (int i=0; i<=15; i++){ try{ Thread.sleep(1000); progressBarDialog.incrementProgressBy((int)(5)); } catch(InterruptedException e){ e.printStackTrace(); } } //dismiss the dialog progressBarDialog.dismiss(); } }); //show the dialog progressBarDialog.show();
The cancel button should reject the dialog.
source share