Why ProgressDialog is not always turning in my android application? - android

I have made a image downloading thread to download image from desire web address. On that thread I have used a progress dialog , but the progress dialog is not turning after 3 or 4 second, it seems that, it is hanged. But the background work is ok. My problem is , what the progress dialog is not turning ? what it is looking like hang?
I am using this code at the start position.
imageUploadhandler.postDelayed(runImageUpload, 500);
dialog = ProgressDialog.show(AllProductActivityPictGrid.this, "",
"Message...", true);
dialog.setCancelable(true);

Your dialog hangs, because if you instantiated your Handler in an Activity, then everything you post to the Handler will run on the UI thread, not on a background Thread.
Do your downloading and create the ProgressDialog in an AsyncTask.

Maybe you should take a look at AsyncTask ?
protected void onPreExecute() {
progressDialog=ProgressDialog.show(context,"Please Wait..","Retrieving data from device",false);
}
#Override
protected String doInBackground(String... params) {
//background stuff here
return "";
}
protected void onPostExecute(String result) {
progressDialog.dismiss();
Toast.makeText(context, "Finished", Toast.LENGTH_LONG).show();
}

Try to use dialog.dismiss() after you your download is completed.
Please use following code to call Progress Dialog.
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Please Wait...");
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.show();
After completion of your task call progressDialog.dismiss();

Related

Android : Progress Dialog not working

I wanted to show a ProgressDialog when data need to be uploaded to the server.
I have checked this question Best way to show a loading/progress indicator?
the best answer was
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
// To dismiss the dialog
progress.dismiss();
when i tried to implement this in my code, nothing showed at all !!
what is that i am doing wrong here?!
this is my code
private void UpdateData()
{
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.show();
try
{
UpdateWSTask updateWSTask = new UpdateWSTask();
String Resp = updateWSTask.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
progress.dismiss();
}
The proper way of showing a ProgressDialog with a AsyncTask would be by displaying the dialog on the onPreExecute() method of the AsyncTask and hide it at onPostExecute() method of it:
private class SampleTask extends AsyncTask<Void, Integer, String> {
ProgressDialog progress = new ProgressDialog(YourActivity.this);
protected Long doInBackground(Void... urls) {
// execute the background task
}
protected void onPreExecute(){
// show the dialog
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setIndeterminate(true);
progress.show();
}
protected void onPostExecute(String result) {
progress.hide();
}
}
Both: onPreExecute() and onPostExecute() run on the main thread, while doInBackground() as the name suggests is executed on the background thread.
Edit:
Within your activity, where you want to call the AsyncTask you just need to execute it:
UpdateWSTask updateWSTask = new UpdateWSTask();
updateWSTask.execute();
A better idea would be to do this with AsyncTask class. You can take care of UI work in preExecute and postExecute methods and do your main work in doInBackground method. Nice and clean!
It seems that you already doing this. Move the dialog code to the asynctask class. You just need a reference to the context and you can provide it with a constructor for your custom asynctask class

Progress bar on a button click

In my app, at the click of a button, the app does some processing, sends some data over the network and then stops. As it takes some time, I tried putting in a progress bar. The code for the progress bar is at the beginning of the onclick listener for the button. After the progress bar code, the processing and sending data over the network takes place.
But the progress bar is not visible at all. Do I need to necessarily show the progress bar in a separate thread?
This is what i have used to show the progress bar
final ProgressDialog pd=new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Please wait..");
pd.setCancelable(true);
pd.setIndeterminate(true);
pd.show();
use the AsyncTask class which was used to do process in background and you can also showing up your progressbar there
Here is the simple snippet of code for AsyncTask
class BackgroundProcess extends AsyncTask<Void,Void,Void>{
private ProgressDialog progress;
public doInBackground(Void...arg){
publishProgress();
// do your processing here like sending data or downloading etc.
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
progress = ProgressDialog.show(YourActivity.this, "", "Wait...");
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if(progress!=null)
progress.dismiss();
progress = null;
}
}
now initialize and execute it in button onclick listener like this way
new BackgroundProcess().execute();
now progressdialog will be publish and appear on the screen and when process was completed then from the onPostExecute() just dismiss the progress dialog
I pasted your code into my application, and it works just fine. Are you calling all of that from the UI Thread? If you are doing some heavy processing and data transmission, make sure not to run that on the UI thread. Make an AsyncTask to handle the network stuff.
EDIT: I moved it into its own thread, and it no longer works, so make sure it's being called from the UI Thread.

ProgressDialog in AsyncTask not updating or dismissing

I am using an AsyncTask to handle complex background operations (compiling a log file to send) and I use a ProgressDialog to show the user progress. I have tried using showDialog() but it never seems to show or dismiss (it is never called), and I followed tutorials on how to do it...
So I am using unmanaged ones, and it won't dismiss my message. I am also wanting to update the message as it starts to compile the log file (as it seems to lag there - or maybe the text view is just really long so it doesn't update like it is supposed to).
I have moved my code around a bit so it look like there are problems (like onProgressUpdate()), but I don't know how to make it work. I have looked around this site and nothing seems to be having the problem I am (not exactly anyways). RunOnUiThread() doesn't work, new Thread(){} doesn't work, and onProgressUpdate() I can't get to work (the documentation is confusing on this).
It also never dismisses. I have set up a listener and it never dismisses.
Does anyone know what is wrong with my code? I thought AsyncTask was supposed to be simple.
private class BuildLogTask extends AsyncTask<Void, Void, String> {
String temp;
ProgressDialog progressdialog = new ProgressDialog(context); //variable is defined at onCreate (held as private, not static)
#Override
protected String doInBackground(Void... params) {
temp = buildLog();
logdata = temp;
publishProgress();
createLogFile();
return temp;
}
protected void onProgressUpdate() {
progressdialog.setMessage("Compiling Log File...");
}
#Override
protected void onPreExecute() {
Log.w(TAG,"Showing Dialog");
send.setEnabled(false);
ProgressDialog progressdialog = new ProgressDialog(context);
progressdialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressdialog.setMessage("Gathering Data...");
progressdialog.setCancelable(false);
progressdialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
Log.e(TAG,"Progress Dialog dismissal.");
}
});
progressdialog.show();
}
#Override
protected void onCancelled(){
Log.e(TAG,"Progress Dialog was Cancelled");
progressdialog.dismiss();
logdata=null;
}
#Override
protected void onPostExecute(String result) {
progressdialog.dismiss();
send.setEnabled(true);
previewAndSend();
}
}
You have two different progress dialogs there, one local to onPreExecute() and one global. The one ur dismissing in your onPostExecution() is your global one which was never actually shown. Remove the local declaration and it should work.
There are two problems.
The signature for on onProgressUpdate is not correct. Try this instead:
#Override
protected void onProgressUpdate(Void... progress) {
progressdialog.setMessage("Compiling Log File...");
}
You're masking the progressDialog member variable with a local variable in onPreExecute()
EDIT: Second problem identified:
Try it,
Replace:
progressdialog.show();
For:
progressdialog = progressdialog.show();
Good luck.

progress dialog not showing in android?

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

Show ProgressDialog while uithread sleeps?

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);

Categories

Resources