Hi I try show progress dialog when mymethod is running with code that below. When thread starts the progress dialog stop I dismiss it at the end of the method that called in thread. How can I solve this?
>mDialog = new ProgressDialog(DealActivity.this);
mDialog.setMessage("Loading...");
mDialog.setCancelable(false);
mDialog.show();
mDialog.setProgress(0);
new Thread() {
public void run() {
mymethod();
}
}.start();
Use AsyncTask instead of thread. Show your ProgressDialog in onPreExecute() method call your mymethod() in doInBackGround() and dismiss you progressDialog in onPostExecute().
Follow this tutorial for better explanation.
Android Threads, Handlers and AsyncTask
You'll also find this tutorial related to your need.
Managing AsyncTask, progress dialog and screen orientation
Related
I've found that a lot of people are having a similar problem but I am simply trying to show a dialog while I am grabbing some data off a URL and then dismiss properly after the data is retrieved. Here is what I'm trying to do (This is in my onClick() method for a refresh button):
dialog.show();
// do some work
dialog.dismiss();
Doing it this way you never really see the dialog at all. I've tried doing it using an extra thread such as:
Thread t = new Thread() {
public void run() {
dialog.show();
}
};
But this way I get an error and a force close down...
What is the best method to do this?
For the task you are trying to implement:
dialog.show();
// do some work
dialog.dismiss();
Now, to implement above, there is concept of AsyncTask, the best way to implement Threading task, as its also known as Painless Threading in Android.
AsyncTask has 4 main methods:
onPreExecute() - Here you can show ProgressDialog or ProgressBar.
doInBackground() - Here you can do/implement background task
onProgressUpdate() - Here you can update UI based on the intermediate result you receive from webservice call or from background task
onPostExecute() - Here you can dismiss dialog or make progress bar invisible. Also you can do the task which want to do after receiving result from background task or webservice call.
For this kind of task mostly I recommend you to use the AsyncTask. Here is one good example of it which will help you.
And for this question as You are making UI related task and for that please use the UI thread.
runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
//Show or Hide your ProgressDialog
}
});
Try to use runOnUiThread of the Activity instead of using Thread.
Check the following code snippet.
runOnUiThread(new Runnable() {
public void run() {
dialog = new ProgressDialog(ctContext);
dialog.show();
}
}
});
I think this will help you.
I'm using a thread so that I can show a progress dialog while my app loads some data. If there is an error it will stop the progress dialog and show the popup saying "error". However I found out that alert dialogs cannot run inside a UI thread and that I need to use a Handler. Can someone with help with this issue? Here is my code. Thanks
verifyCode.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
final ProgressDialog progressDialog = ProgressDialog.show(
Activate.this, "", "Loading...");
new Thread(new Runnable() {
public void run() {
try {
new AlertDialog.Builder(Activate.this)
.setTitle(getResources().getString(R.string.InvalidKey))
.setMessage(getResources().getString(
R.string.PleaseEntervalidRegistration)).setNeutralButton(
"OK", null).show();
progressDialog.dismiss();
//more code
}).start();
}
You can't make changes to UI element on non-UI threads. onClick will run on the UI thread, but since you spawn a Thread inside onClick then non-UI elements cannot be manipulated from inside that Thread. Move your AlertDialog and ProgressDialog calls to just prior to spawning the new Thread.
Also, as #lightblade suggested, If you need to do some sort of action which requires heavy background processing and UI manipulation based on that processing, then you should use AsyncTasks. It provides methods you can override for pre-processing, actual processing, post-processing, and updating progress.
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
When I define progress dialog functions such as
public static void showLoadingBar(Context context)
{
dialog=new ProgressDialog(context);
dialog.setMessage("Please wait");
dialog.show();
}
public static void hideLoadingBar()
{
dialog.dismiss();
}
I wanna use it like following:
UiManager.getInstance().showLoadingBar(this);
FetchData();
UiManager.getInstance().hideLoadingBar();
But I have never be able to show LoadingBar until I comment the
UiManager.getInstance().hideLoadingBar(); line such like that
UiManager.getInstance().showLoadingBar(this);
FetchData();
//UiManager.getInstance().hideLoadingBar();
What this cause is, always ProgressBar on the screen. Is there anyway to get rid of this issue?
FetchData() seems to be an asynchronous operation. So, before the actual operation is complete, the function returns and hides the loading bar. I suggest you use an AsyncTask.
To show a progress dialog while an AsyncTask runs, you may call show() in onPreExecute() and call hide() in onPostExecute(). Call FetchData() from doInBackground(). This will start the ProgressDialog before the AsyncTask does it's background method and will stop the ProgressDialog when it completes.
I have a simple dialog box. When I click a button the dialog box is supposed to be shown, while a file save operation is performed, and then the dialog box is dismissed. The problem I am having is the dialog box isn't shown until after the onClick event of the button finishes.
Taken from the Dialog developer doc:
The setup is simple. Most of the code
needed to create a progress dialog is
actually involved in the process that
updates it. You might find that it's
necessary to create a second thread in
your application for this work and
then report the progress back to the
Activity's UI thread with a Handler
object. If you're not familiar with
using additional threads with a
Handler, see the example Activity
below that uses a second thread to
increment a progress dialog managed by
the Activity.
Why isn't the dialog shown until after the onClick method finishes? Is the dialog added to the end of the UI thread?
Is the only way to do this to create a new thread and handler? That's fairly bad wording in the developer doc if so.
Thanks all.
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
showDialog(SAVING_DIALOG);
//Do all the file saving operations
...
...
dismissDialog(SAVING_DIALOG);
}
});
Here is the dialog
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SAVING_DIALOG: {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Saving file...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
return dialog;
}
}
return null;
}
Shouldn't work be done on a thread when using the progress dialog?
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog
For showing a progress dialog while something is processing, you have to process your code in another thread, otherwise your dialog freezes or wont be shown.
So I would use following method:
final Handler threadHandler = new Handler();
// in your onClick:
showDialog(SAVING_DIALOG);
new Thread(){
public void run(){
// new thread
// Do all the file saving operations
// ...
threadHandler.post(new Runnable(){public void run(){
// back in UI thread
dismissDialog(SAVING_DIALOG);
}});
}
}.start();