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();
Related
When my app is used for the first time i am performing some data setup etc that takes a "random" time to complete, whilst this is going on i'm showing a progress dialog to the user telling them whats going on and presenting a spinning wheel, this is all done via an async task as the many docs and guides say is the only way to go about using a progress dialog.
but my problem is i need everything else in my app to "wait" for the data setup to be finished before it goes about its business but still keep the handy dialog telling the user whats going on, im struggling to find how to go about this.
if anyone has any ideas that be great.
You can use Threads for doing data setup , and handlers for implementation after the work in thread is over..
See Below
ProgressDialog pd = ProgressDialog.show(this,"Please Wait..", "Data Setup in Progress..", false, true);
pd.setCanceledOnTouchOutside(false);
Thread tDataSetup = new Thread(
new Runnable()
{
#Override
public void run()
{
//Insert the data setup code here..
if(dataSetupDone)
handlerDataSetup.sendEmptyMessage(OPERATION_COMPLETED);
else
handlerDataSetup.sendEmptyMessage(OPERATION_NOT_COMPLETED);
}
});
tDataSetup.start();
private Handler handlerDataSetup = new Handler()
{
#Override
public void handleMessage(Message msg)
{
if(pd.isShowing())
{
pd.dismiss();
}
if(msg.what == OPERATION_COMPLETED)
//Code after the data Setup done , to be implemented here..
else if(msg.what == OPERATION_NOT_COMPLETED)
//Code if data setup fails..
}
};
Something like this should work:
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
// your vairiable time stuff here
// To dismiss the dialog
progress.dismiss();
if you need to update your progress dialog after finishing some work you can do something like
progress.setMessage("I have a new message now");
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
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().
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.
This is what I got:
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
(...)
ProgressDialog pd = new ProgressDialog(MyApp.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
// now fetch the results
(...long time calculations here...)
// remove progress dialog
pd.dismiss();
I've also tried adding pd.show(); and messed around with the parameter in new ProgressDialog resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.
I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm() in MyApp.
Any hint is appreciated,
you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
here you can see an example:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
#Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
#Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
I am giving you a solution for it,
try this...
First define the Progress Dialog in the Activity before onCreate() method
private ProgressDialog progressDialog;
Now in the onCreate method you might have the Any button click on which you will change the Activity on any action. Just set the Progress Bar there.
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use thread to handle the Progress Bar to Display and hide
new Thread()
{
public void run()
{
try
{
sleep(1500);
// do the background process or any work that takes time to see progress dialog
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progress dialog
progressDialog.dismiss();
}
}.start();
That is all!
Progress Dialog doesn't show because you have to use a separated thread. The best practices in Android is to use AsyncTask ( highly recommended ).
See also this answer.
This is also possible by using AsyncTask. This class creates a thread for you. You should subclass it and fill in the doInBackground(...) method.
Is there a standard "Loading, please wait" dialog I can use in Android development, when I invoke some AsyncTask (downloading some data from remote service for example)?
You mean something like an indeterminate ProgressDialog?
Edit: i.e.
ProgressDialog dialog = ProgressDialog.show(context, "Loading", "Please wait...", true);
then call dialog.dismiss() when done.
If you implement runnable as well as extending Activity then you could handle the code like this...
private ProgressDialog pDialog;
public void downloadData() {
pDialog = ProgressDialog.show(this, "Downloading Data..", "Please wait", true,false);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
// add downloading code here
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
pDialog().dismiss();
// handle the result here
}
};
It's worth mentioning that you can set the content view of the progress dialog so you can display a custom message / image:)
pDialog.setContentView(R.layout.X);
Mirko is basically correct, however there are two things to note:
ProgressDialog.show() is a shortcut that automatically creates a dialog. Unlike other dialogs, it should NOT be used in onCreateDialog(), as it will cause errors in Android 1.5.
There are some further issues with AsyncTask + ProgressDialog + screen orientation changes that you should be aware of - check this out.