Progressbar not stopping - android

I made a progress bar to run for 5 second.it showing perfectly for first time.but for second time it not stopping after 5 second.it keeps running.
Code is here
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Finding Nearest Officer...");
pDialog.setTitle("Please Wait...");
pDialog.setIndeterminate(false);
//pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setCancelable(false);
pDialog.show();
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
pDialog.dismiss();
//delayInMillis=5000;
}
}, 5000);
return pDialog;
default:
return pDialog;
}
}

I assume that you need another callback method to be overriden
#Override
protected void onPrepareDialog(int id, Dialog dialog) {
// perform scheduling for dismiss here
super.onPrepareDialog(id, dialog);
}
In other words - onCreateDialog() is being called only first time, but onPrepareDialog is being called each time before you going to show it, so all your scheduling is better to setup inside onPrepareDialog().
Also it is important to notice that all these APIs are deprecated and it is better to take a look at DialogFragment or something similar.

I think you make object from this method like this:
Dialog dialog = onCreateDialog(progress_bar_type);
And call Dialog by that aboject like this:
dialog.show();
but instead of that please call only method where you want that dialog to show.
like this:
onCreateDialog(progress_bar_type);
when ever you want to show dialog please only call this method directly.
you will get same output every time.

Related

Why ProgressDialog is not always turning in my android application?

I have made a image downloading thread to download image from desire web address. On that thread I have used a progress dialog , but the progress dialog is not turning after 3 or 4 second, it seems that, it is hanged. But the background work is ok. My problem is , what the progress dialog is not turning ? what it is looking like hang?
I am using this code at the start position.
imageUploadhandler.postDelayed(runImageUpload, 500);
dialog = ProgressDialog.show(AllProductActivityPictGrid.this, "",
"Message...", true);
dialog.setCancelable(true);
Your dialog hangs, because if you instantiated your Handler in an Activity, then everything you post to the Handler will run on the UI thread, not on a background Thread.
Do your downloading and create the ProgressDialog in an AsyncTask.
Maybe you should take a look at AsyncTask ?
protected void onPreExecute() {
progressDialog=ProgressDialog.show(context,"Please Wait..","Retrieving data from device",false);
}
#Override
protected String doInBackground(String... params) {
//background stuff here
return "";
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();
}
Try to use dialog.dismiss() after you your download is completed.
Please use following code to call Progress Dialog.
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Please Wait...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
After completion of your task call progressDialog.dismiss();

progress dialog not showing in android?

when does the progress dialog not show in android? i want to know the circumstances when the above can happen:
in my case the progress dialog was not showing in this case:
func{
progressdialog.show();
....
.....
anotherfunction();
listview.setAdapter();
progressdialog.dismiss();
}
what is the general rule of thumb with dialog boxes?
thank you in advance.
EDIT
when the .show() command is executed the progress dialog should show. But when the otherfucntion() is called, does the previous command of progressdialog show stop?
Seems like you need to use AsyncTask the UI (including the progressDialog) will not update if the UI thread is still busy. There are many examples in SO for that.
And as a rule of thumb - if you need Progress dialog - you need AsyncTask.
It is not that any command stops, it is just that if you execute a sequence of methods on the UI thread, the UI will probably not be updated until the sequence is over, which is after progressDialog.dismiss(), so the progressDialog should not be displayed anymore.
I think You have to do this in your activity.
ProgressDialog _progressDialog = ProgressDialog.show(this,"Saving Data","Please wait......");
settintAdater();
private void settingAdater(){
Thread _thread = new Thread(){
public void run() {
Message _msg = new Message();
_msg.what = 1;
// Do your task where you want to rerieve data to set in adapet
YourCalss.this._handle.sendMessage(_msg);
};
};
_thread.start();
}
Handler _handle = new Handler(){
public void handleMessage(Message msg) {
switch(msg.what){
case 1:
_progressDialog.dismiss();
listview.setAdapter();
}
}
}
To show a ProgressDialog use
ProgressDialog progressDialog = ProgressDialog.show(PrintMain.this, "",
"Uploading Document. Please wait...", true);
And when you have completed your task use
progressDialog.dismiss();
to dismiss the ProgressDialog ..
You can call to show the ProgressDialog in your onPreExecute method of AsyncTask class and when your done dismiss it in the onPostExecute method

Android: Cannot dismiss progressdialog

I am trying to dismiss a progress dialog but it doesnt seem to work for some reason. Any suggestions why is it so??
The progress dialog is initialized on button click and shows the dialog. The syncbutton method calls a thread which sends an empty message
thanks
mHandler.sendEmptyMessage(0);//from thread
Code for button click
public void onClick(View v) {
pd = new ProgressDialog(Screen.this);
pd.setCancelable(true);
ProgressDialog.show(Screen.this, "Sync", "Sync in progress",true,false);
SyncButton();
}
});
Code for message handler which should dismiss the progressbar
mHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
Log.d("Handler","handler");
if (Screen==true){
if (pd !=null)
{
pd.cancel();
pd.dismiss();
Log.d("HANDLER", "called dismiss");
}
}
}
};
PS: I did try using asynctask but was having problems with it. Thats why took this approach. I had posted that question here
Looke like you are creating one progress dialog here:
pd = new ProgressDialog(Screen.this);
pd.setCancelable(true);
That one isn't displayed. You create and display another one:
ProgressDialog.show(Screen.this, "Sync", "Sync in progress",true,false);
Remove the two first lines, and change your other line to:
pd = ProgressDialog.show(Screen.this, "Sync", "Sync in progress",true,false);
You aren't showing the pd you created, you are showing a new one.
build pd
pd.setTitle("Sync");
pd.setMessage("Sync in progress");
then use
pd.show();

ProgressDialog working but not progressing in the icon (wheel)

I am using a simple progressDialog that running ok but the the wheel dose not progress:
//Progress Dialog
final ProgressDialog dialog = ProgressDialog.show(TravelPharm.this, "Searching","Please wait ...", true);
((ProgressDialog) dialog)
.setProgressStyle(ProgressDialog.BUTTON_NEUTRAL);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
what i am missing??
Create your progress dialog like so:
final ProgressDialog progress = new ProgressDialog(context);
add some text/icon to it:
progress.setTitle("Loading");
progress.setMessage("Loading, please wait");
progress.setIcon(R.drawable.icon);
Show it:
progress.show();
I think you should pass ProgressDialog.STYLE_SPINNER to ProgressDialog.setProgressStyle() method.
final ProgressDialog dialog = ProgressDialog.show(TravelPharm.this, "Searching","Please wait ...", true);
The way you are creating the ProgressDialog is correct - if the spinner isn't spinning then something is blocking your UI thread.
Out of interest, why are you using TravelPharm.this for the context instead of this? I'm not sure it's the cause of your problem, I'm just wondering why.
I am guessing that you are launching a time intensive task from a dialog and then trapping the thread exit in your handler where you are trying to dismiss the dialog. If possible, consider simply sending an empty message when the dialog is done. Then in the handler create a new AsyncTask as:
private class MyAsynch extends AsyncTask<String, Void, String>{
protected void onPreExecute() {
resetProgress();
progress.show();
}
#Override
protected String doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE
// TODO Auto-generated method stub
doNonUIStuff();
return someString; // <== return value String result is sent to onPostExecute
}
protected void onPostExecute(String result){
progress.dismiss();
doSomethingWithString(result); // you could launch results dialog here
}
};
protected void onPause() {
super.onPause();
if (asynch != null) {asynch.cancel(true);}
if (progress != null){progress.cancel();}
}
private void resetProgress() { // avoid frozen progress dialog on soft kill
if (progress != null && progress.isShowing()){
progress.cancel();
}
progress= new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setMessage("I am thinking.");
}
You could return any type in onPostExecute, in this example I am returning a string. Another approach would be to launch a second Activity as a "dialog" using startActivityForResult create the AsycnTask in onActivityResult.
In other words, gather the data in a dialog or second Activity, then in the first activity show a progress dialog in onPreExecute, do the time intensive task in the background, and cancel the progress dialog in onPostExecute.
I have seen the frozen spinning ball, thus the call to resetProgress().

Android: Progress Dialog Doesn't Show

I have some data I load into the database the first time a user enters my Activity, and want to show a ProgressDialog while this data is loaded for the first time. My Activity is an ExpandableListActivity and I don't create the SimpleExpandableListAdapter or call setListAdapter passing my adapter until I'm sure the data is actually there. My onCreate looks like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCategoryDbHelper = new CategoryDBHelper(this);
// Build default categories... if not there yet
CategoryDbBuilder builder = new CategoryDbBuilder(this);
if (!builder.hasCategoriesInTable()) {
showDialog(PROGRESS_DIALOG_ID);
builder.fillDbWithDefaultCategories();
removeDialog(PROGRESS_DIALOG_ID);
}
populate();
}
I've overwritten onCreateDialog as such:
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case PROGRESS_DIALOG_ID: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading categories for the first time. Please wait...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
return dialog;
}
}
return null;
}
The populate() method reads the database and sets up my list adapter, then calls setListAdapter.
This seems like it should be simple, but it's turning out to be a huge pain. Any help would be appreciated. :-)
Use AsynTask put your database loading processing in background function and in post execution display result. and there is another function to processing something until background process running here is example of asynTask
Android - I want to show file upload progress to the user
Just use this simple line:
mProgressDialog = ProgressDialog.show(context, "", "msg to show", true);
You can dismiss it with:
mProgressDialog.dismiss();

Categories

Resources