I've got a fairly simple issue I'm not sure how to resolve. I want to change the message text of a Progress Dialog while it's running and showing. An example of this would be something like a "time remaining" counter that would count down while the progress animation is spinning. I'm not sure how I would approach this because doing progressDialog.setMessage(String), even within a running thread, will not change anything in the Dialog itself. Thank you!
You might want to check this question out.
In short: You need to create a little Runnable which you can execute on the UI thread using runOnUiThread...
Related
I'm developing an Android app that has a basic structure: activity that requests some action from AsyncTask implementer. The implementer has 3 custom methods that should be able to update UI thread with a Dialog and a postExecute() that should update UI thread with a failuer Dialog if an exception is thrown. Here are some questions:
Where should I create the Dialog object? In the activity class or the AsyncTask implementer? What general guidelines should I follow?
Can I update UI thread with a Dialog without waiting for postExecute()?
How can I update UI thread with a picture? Should I create a custom Dialog or is there an easier way?
If the updates - as dialogs with pictures - come one after another, in a sequence how should I deal with it? Should I create some kind of queue? How would you do it?
Thank you in advance :)
1)There's a couple of different ways you can do this. But personally I usually create the dialog in onPreExecute of the AsyncTask, so that the UI for the task is completely self contained.
2)Yes. You can do it in onProgressUpdate. doInBackground should call publishProgress() which will cause onProgressUpdate to be called on the UI thread.
3)Too few details- where do you want the picture? In an existing image view? On top of the current layout? If you just want to display it in a dialog box, an AlertDialog with custom layout would probably work.
4)Depends on the app. Do you want the user to see all the images, or is it ok to miss images in the middle if a new one is sent?
I have an AsyncTask that calls a native method, and I want to report the progress.
Is it possible? I can't change the Native since its a black box to me, but I can read its output which is a file, which I can parse as progress.
Thanks,
Eli
As much as I know, its not possible using async task, but its doable using combination of threads and handler.
you can use this pattern:
Use a Class with 2 threads, one for the worker and one for the progress report.
The last updates the progress bar (which is a member).
When finished, call the handler message method to dismiss the progress bar.
You can grab the code from here.
Eli
I am doing a test project using robotium. I need to pause excecution of all the remaining code till the progress dialog is closed. Now i am hardcoding the time. But the problem is sometimes progress dialog will not close within that time period.how can i do this?
You can use the waitForDialogToClose(long timeout) method.
Hope this helps.
I am creating an app MainActivity that can calculate a report from its database to be shown in ReportActivity. All report calculation is done in ReportActivity.onCreate().
When the user click on MainActivity-s menu "generate report" the menĂ¼ remains open for 2 secs until ReportActivity.onCreate() has finished and the report becomes visible.
What is the easiest way to give the user some visual feedback?
I already found ProgressDialog onCreate that shows a progressdialog while doing all calculation in an AsyncTask.
I wonder if there is some easier way to give the user some visual feedbak that the menue was successfully clicked and that the device has not crashed.
in ms windows i would use a waitcursor (hour-glass)
I already tried to open a ProgressDialog() in oncreate of the ReportActivity
final ProgressDialog progressDialog = ProgressDialog.show(this, "MY Dialog", "Please wait...");
but it is not shown at all.
Update final solution:
As the answerss suggested I implemented an AsyncTask for the processing following this tutorial
based on this codesample
You need to move your long-running code into a background thread. Long-running code on the UI thread will lock up the UI, which causes the user to think the app is frozen/crashing. Look into using AsyncTask. This allows you to run a process on a background thread and also give visual feedback to let the user know that your app is working.
Design a layout with ProgressDialog. Use setContentView(Progressdialog). Count time for 2-4 seconds (you choice) again set your older layout in setContentView. Why don't you use AsyncTask?
Use a bar or circle activity indicator. From the official design guide:
Activity indicators are for operations of an indeterminate length.
They ask users to wait a moment while something finishes up, without
getting into specifics about what's happening behind the scenes.
I have an issue with my ProgressDialog object. When I show it first time, it is spinning, but after I dismiss it and show it again, the dialog is static, it it not spinning... Can you please tell me why? All of this happens in the UI thread. Thanks
are you doing any processing in the UI thread during the second show of the ProgressDialog? If so, you need to move the processing to a background thread. Otherwise, the progress dialog does not get the CPU to update(spin) itself.