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().
Related
I've been using dialog for the Asynctask progress to show. By doing this, I initialized it inside onPreExecute and dismissed it in onPostExecute. I'm not sure what went wrong when I placed a condition to dismiss only if it's not null and it's showing but still triggered IllegalArgumentException: View not attached to window manager
Here's the code:
public class SampleTask extends AsyncTask<String, Void, String> {
public ProgressDialog pDialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(SampleActivity.this);
pDialog.setMessage(getString(R.string.loading));
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... path) {
.
.
.
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(null);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
.
.
.
}
}
I read here similar solution but would it differ if I place null != pDialog && pDialog.isShowing() together in the same condition?
Your problem is you only accept the android view in main UI Thread. Read this link for more detail So when you call pDialog.dismiss(); in onPostExecute(String), it run on another thread so android throw new IllegalArgumentException. I have some solution for you and hope that help:
1. I alway use this solution but it not good at all:
Handler handler = new Handler();
Runnable runable = new Runnable() {
#Override
public void run() {
// TODO something you would like to do with Android UI.
}
};
handler.post(runable);
The Handler will create a new thread to run on UI Thread so you can work with Android UI.
2. Sometime I try to use an interface. It more effective than the solution above however, of course, it make me spend more effort.
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 want to show ProgressDialog while uithread sleeps so that until the data from the server is retrived my activity will not be shown. How can I do this?
You can use Thread, AsyncTask, or Service to load your data in the background, and with a Handler implementation control your ProgressDialog.
The example in this post shows how to use a thread for a login request, and in the meantime show the progress dialog.
Using AsyncTask is a lot easier and clearer:
private static final int WAIT = 11;
private final class MyTask extends AsyncTask<Void, Void, Void>
{
#Override
protected void onPreExecute()
{
super.onPreExecute();
// Show up the dialog with id=WAIT [11]
showDialog(WAIT);
// other actions that must be performed in the UI thread
// before the background works starts
}
#Override
protected Void doInBackground(Void... params)
{
// perform the background work
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
// Remove the dialog with id=WAIT [11]
removeDialog(WAIT);
// other actions that must be performed in the UI thread
// after the background works finished
}
}
[...]
final MyTask task = new MyTask();
task.execute(null);
Since AsyncTask is a generic type, you can specify the parameter types for your preference, so it is very handy for transferring data from the ui thread to a background thread and back.
Your dialog part is just a few lines inside your activity:
private ProgressDialog dialog;
#Override
protected Dialog onCreateDialog(int id)
{
switch (id)
{
case WAIT:
{
dialog = new ProgressDialog(this);
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
return dialog;
}
}
return null;
}
This task is commonly solved with AsyncTask bounded with progress dialog. See this article.
Move your Network Process code into a Thread and get a ProgressDialog. Start your network process by calling .start(); and then ProgressDialog.show(); when you have done in network process, stop the ProgressDialog through a Handler from Thread.run().
you can try this code for progress dialoge in ur thread
ProgressDialoge pd = ProgressDialog.show(this, "Please wait...", "Retrieving data.", true,false);
-->I am new to Android And i want to show two progress dialog one after another??
-->First i want to show when my image is load from internet, when this process is done i have set A button on that Remote image.
-->When i click that button i want Dialog for second time..(on clicking button i have set video streaming code.. before video is start i want to close that Dialog..)
Any Help????
Thanks...
You can create 2 threads. First for image when it is loaded then call another thread of video.
Make two runnable actions and two handlers to handle that
public void onCreate(Bundle savedInstanceState) {
ProgressDialog pd = ProgressDialog.show(this, "","Please Wait", true, false);
Thread th = new Thread(setImage);
th.start();
}
public Runnable setImage = new Runnable() {
public void run() {
//your code
handler.sendEmptyMessage(0);
}
};
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (pd != null)
pd.dismiss();
}
};
On Android it's best to use AsyncTask to execute tasks in the background while still updating UI:
Extend the AsyncTask
Start the progress dialog in onPreExecute()
Define the background task in doInBackground(Params...)
Define updating of the progress dialog in onProgressUpdate(Progress...)
Update dialog by calling publishProgress() from doInBackground()
Start a new Dialog #2 in onPostExecute().
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.