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);
}
Related
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
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
}
};
I'm developing an Android Game.In this game, There are tracks on which trains run. This is running thread. I want to show an alert dialog when there is a collision between. when I'm applying alert dialog showing error can't create handler inside thread that has not called looper.prepare().
runOnUiThread(new Runnable() {
#Override
public void run() {
// Your dialog code.
}
});
You must need to create AlertDialog inside UI thread else it will never work. If you are in different thread use MessageHandler or can use runOnUiThread(using runnable) to create your dialog inside.
You can use Handlers to do this work.
Handler mHandler = new Handler(Looper.getMainLooper());
mHandler.post(new Runnable() {
#Override
public void run() {
// Your UI updates here
}
});
You can create an handler in Activity Class, and can invoke sendMessage to that handler object. Write code to display alert in handleMessage method of Handler, for Example:
Activity Class
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
//Display Alert
}
};
//Thread
Thread thread= new Thread()
{
public void run()
{
//Logic
MHandler.sendEmptyMessage(0);
}
}
You have to show your dialog on UI thread like below
runOnUiThread(new Runnable() {
#Override
public void run() {
// Your dialog code.
}
});
You can try this, with check App is visible
Activity currentActivity = MainClassApp.getCurrentActivity();
boolean isAppVisible = currentActivity != null;
if (isAppVisible) {
currentActivity.runOnUiThread(() ->
// Your Dialog Code
}
I am new to Android dev, and am trying to solve this problem that has been giving me some frustration. I am trying to close this progressDialog. When I run the app, it displays, the information is fetched, and the UI is updated. However, the dialog is never dismissed.
progDialog = ProgressDialog.show(HomeActivity.this, "", "Fetching info...", true, false);
new Thread(new Runnable() {
public void run() {
fetchInfomation(userID); //fetches information - Works
runOnUiThread(new Runnable() {
public void run() {
setLayoutList(); //updates UI - Works
progDialog.dismiss(); //doesn't seem to close progress dialog
firstView(); //displays prompt - Works
}
});
progDialog.dismiss(); //doesn't close dialog either
}
}).start();
Any ideas?
You can't interact with UI inside an external thread. There is some techniques to do that but it's not necessary.
You can use Handler.
For example:
...
private Handler mHandler;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mHandler = new Handler() {
#Override
public void handleMessage (Message msg) {
switch (msg.what) {
case 0: ...
case 1: ...
}
}
}
}
And:
....
new Thread() {
#Override
public void run() {
... // do your background jobs here
mHandler.sendEmptyMessage(...); // interact with UI
});
}
}.start();
It will be good practice if you do any GUI updates on UI thread. Inside any thread you can run your UI thread where you can do the UI stuffs or else you can go for message handler also which will do the same for you.
Runnable run_in_ui = new Runnable() {
#Override
public void run() {
// do your UI stuffs here
}
};
runOnUiThread(run_in_ui);
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();
}
});