ProgressDialog is not showing in android - android

I know that is easy to do it, but I've tried to take some examples from here and is not showing the progressDialog. That I want to do is show a ProgressDialog when click the button and finish when the task is finished:
Thanks in advance!
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
}
});
};
}).start();
progress.dismiss();
}
});

Your ProgressDialog is showing but its dismissed right after the show. Put your dismiss inside the run method of your runnable.
This way:
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
progress.dismiss();
}
});
};
}).start();
}
});

a thread is an independent process which runs in background your progress bar is showing and dismissing immediately, call progress.dismiss(); in your run() after your onPhotoTaken()

in below code you dismiss the dialog as it start also running thread so dismissing dialog can't wait for task which is inside thread.
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
progress.dismiss();
}
});
};
}).start();
}
});
onPhotoTaken() :- should return any value so that we can dismiss dialog.
and dismiss dialog in main thraed

Related

Starting and stopping progressbar on UI thread from class

Is there a slicker/simpler way of doing the following? I have a method in a class that shows a progressbar while a thread runs. This code works but it just seems a little overly clunky having 3 steps.
private void pause() {
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
}
This does not show a progress bar while a thread runs.
Your thread can run 10 seconds but the visibility of the ProgressBar will only blink if you can see it at all.
Instead, you should hide only once the thread has completed, so this would be correct:
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.VISIBLE);
}
});
new Thread(new Runnable() {
#Override
public void run() {
try {
//do stuff
mActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
For a "slicker" way, you could use an AsyncTask which was created for this very task. Example:
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.VISIBLE);
}
#Override
protected Void doInBackground(Void... params) {
// Do stuff
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// Guaranteed to run on the UI thread
mProgressBar.setVisibility(View.GONE);
}
}.execute();
Here you can use the handlers concept to communicate with UI Thread.
I.e,
Handler handler=new Handler(){
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch(msg.what){
case 1:
mProgressBar.setVisibility(View.VISIBLE);
break;
case 2:
mProgressBar.setVisibility(View.GONE);
break;
}
}
}
new Thread(new Runnable() {
#Override
public void run() {
Message processStart = handler.obtainMessage(1);
processStart.sendToTarget();
try {
//do stuff
} catch (InterruptedException e) {
e.printStackTrace();
}
Message processStart = handler.obtainMessage(2);
processStart.sendToTarget();
}
}).start();
I hope this one will helps you :)

Android Dialog open before running Thread.sleep(1000) loop

I am trying to open a dialog box before it runs a method in dosubmit() with a Thread.sleep(1000) loop that is checking for the result of a RunnableFuture.
It seems overriding the setOnShowListener is not working.
private void dosubmitdialog(){
mDialogloading = new ProgressDialog(mActivity);
mDialogloading.setCanceledOnTouchOutside(false);
mDialogloading.setCancelable(false);
mDialogloading.setMessage("Uploading data ...");
mDialogloading.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
dosubmit();
}
});
mDialogloading.show();
}
What other approach can I use?
Instead of using Thread.sleep(1000) use Handler.postDelayed() to call dosubmit(); after 1000 as:
public void onShow(DialogInterface dialog) {
Handler handler=new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
handler.removeCallbacksAndMessages(null);
dosubmit();
}
}, 1000);
}

How to execute an instruction/method immediately after ProgressDialog dismisses?

I am trying to execute the method doSomeWork(); after the ProgressDialog dismisses in my method printing();which seems to be overlapped by the other method and the dialog is not showed up. If I comment method doSomeWork(); the dialog is displayed correctly until the thread is finished.
Here is my method printing();
public void printing()
{
final ProgressDialog printingDialog = ProgressDialog.show(this, "Printing...", "Please wait", true, false);
new Thread(new Runnable() {
#Override
public void run()
{
//something big executing here
}
}).start();
}
He is my method doSomework():
public void doSomeWork(){
Thread receiptPrint = new Thread(new Runnable() {
#Override
public void run() {
//something here
runOnUiThread(new Runnable() {
#Override
public void run() {
//another dialog here
}
});
}
});
}
Here you can see the how I am calling those two methods:
private OnClickListener onClickPrint = new OnClickListener() {
public void onClick(final View v) {
Log.d("Button","Clicked on Print Order Button");
printing();
doSomeWork();
Does anyone know how could I execute doSomeWork() only when printing(); will be completely finished?
This is one of the purposes of an AsyncTask. It would look similar to this:
public void onClick(final View v) {
new AsyncTask<Void, Void, Void>() {
#Override
protected void onPreExecute() {
//Show your progress dialog in here
super.onPreExecute();
}
#Override
protected Void doInBackground( Void... params ) {
printing();
return null;
}
#Override
protected void onPostExecute( Void result ) {
//Dismiss your progress dialog here
doSomeWork();
}
}.execute();
}
Instead of using thread you can use asynchronous task. Show the progress dialog in the preexecute method call the printing method inside the background method after completing printing operation call the doSomeWork() inside the postexecute method.
You can use Handler for that in android. for example consider the following piece of code. you can dismiss the dialogs inside handlers. may it work for you.
private void printing(){
Thread receiptPrint = new Thread(new Runnable() {
#Override
public void run() {
retrieveEmails();
runOnUiThread(new Runnable() {
#Override
public void run() {
try{
//here your code executes
//after code executes do following:
uiHandler.sendEmptyMessage(0);
}catch(Exception ex){
errorHandler.sendEmptyMessage(0);
}
}
});
}
});
receiptPrint.start();
}
final Handler uiHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
//here execute doSomeWork()
}
};
final Handler errorHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
//do other stuff
}
};

ProgressDialog in Android not appearing (incorrect thread handling?)

I am trying to implement a radio player (using shoutcast streams) for android. What I want to do is, while the radio stream loads in the player, the UI displays a spinning wheel animation. On successful loading (as soon as the song starts playing) the animation disappears.
Here is the code that I am using.
PlayStopStreamingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Thread initializer = new Thread(new Runnable() {
#Override
public void run() {
Looper.myLooper();
Looper.prepare();
progressDialog = ProgressDialog.show(RadioPlayerActivity.this, "", "Selecting Radio Station",
true);
JukefoxApplication.getHandler().post(new Runnable() {
#Override
public void run() {
radioPlayerEventListener.onPlayStopStreamingButtonClicked();
progressDialog.dismiss();
}
});
}
});
initializer.start();
}
});
I don't get any spinning animation. I am almost certain that my mistake lies in incorrect handling of threads. If someone could out the correct way, I would be grateful.
EDIT, this seems to work:
PlayStopStreamingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
progressDialog = ProgressDialog.show(RadioPlayerActivity.this, "", "Selecting Radio Station", true);
Thread initializer = new Thread(new Runnable() {
#Override
public void run() {
radioPlayerEventListener.onPlayStopStreamingButtonClicked();
progressDialog.dismiss();
}
});
initializer.start();
}
});
You need to show progress dialog on UI thread, see below:
PlayStopStreamingButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Thread initializer = new Thread(new Runnable() {
#Override
public void run() {
Looper.myLooper();
Looper.prepare();
RadioPlayerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog = ProgressDialog.show(RadioPlayerActivity.this,
"", "Selecting Radio Station", true);
}
});
JukefoxApplication.getHandler().post(new Runnable() {
#Override
public void run() {
radioPlayerEventListener.onPlayStopStreamingButtonClicked();
RadioPlayerActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
}
});
initializer.start();
}
});

Android ProgressDialog stops spinning

I have created a ProgressDialog in my android application. But the problem I am having is during the point where it is actually doing the work it stops spinning the wheel. Here is my code. How can I make it so it continually spins the wheel while my other work is going on?
button5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("Button5");
//Handler to make the please wait message
final ProgressDialog myProgressDialog = ProgressDialog.show(
FoodSubstitutesActivity.this, "Please wait...",
"Getting most recent updates...", true);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
//DO STUFF - STOPS SPINNING WHEEL UNTIL THIS PART IS COMPLETE.
myProgressDialog.dismiss();
}
}, 500);
}
});
Why dont you try doing it this way?
final ProgressDialog dialog = ProgressDialog.show(this, "Title",
"Message", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
//
// YOUR LONG CALCULATION (OR OTHER) GOES HERE
//
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
taken from: http://www.tutorials-android.com/learn/How_to_display_a_progress_dialog_while_computing.rhtml
use this code this may help you,
// TODO Auto-generated method stub
myProgressDialog = ProgressDialog.show(MainActivity.this,
"", "Please wait....");
myProgressDialog
.setProgressStyle(ProgressDialog.STYLE_SPINNER);
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// do your action...........
finish();
}
});
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
Thanks...

Categories

Resources