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.
Related
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();
I am attempting to use AsyncTask to load a file of determinate length. My AsyncTask looks something like this:
protected void onPreExecute() {
dialog = ProgressDialog.show(MyActivity.this, null, "Loading", false);
}
protected void onProgressUpdate(Integer... values) {
if (values.length == 2) {
dialog.setProgress(values[0]);
dialog.setMax(values[1]);
}
}
in my doInBackground() implementation I call publishProgress(bytesSoFar, maxBytes); inside my loading loop and in the onPostExecute() I call dialog.dismiss().
However, I can't get the ProgressDialog to show anything but an indeterminate spinner. I want to see a horizontal progress bar that shows the progress as the loading happens. I've debugged and can see that onProgressUpdate() gets called with sane values and that the dialog's methods are getting called.
Add Style to your progress dialog with before you show it .setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
Use this code in your onPreExecute().
ProgressDialog prog;
prog = new ProgressDialog(ctx);
prog.setTitle(title);
prog.setMessage(msg);
prog.setIndeterminate(false);
prog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
prog.show();
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 want to show the progress bar during web service call. I called progress bar before calling the service, but it is being called after the service call is finished and i have received the response.
ProgressDialog dialog = ProgressDialog.show(LogIn.this,"","Loading. Please wait...", true);
status=Loginvalid(method,username,psword); //calling the method for making service call
But, progress dialog is starting after the response is received from the service.
Please how can i fix this problem..
public class Progress extends AsyncTask<String, Void, Void> {
protected void onPreExecute() {
ProgressDialog dialog = new MyProgressDialog(MyActivity.this, "Loading.. Wait..");
dialog.show();
}
#Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
// do your network connection
return null;
}
protected void onPostExecute(Void unused) {
dialog.dismiss();
}
}
Use AsyncTask. It is the most effective and painless way of showing a progress dialog during a web service call.
show the progressbar on preexecute, call your webservice in doInBackground method, and dismiss the progressbar onPostexecute.
http://developer.android.com/reference/android/os/AsyncTask.html
In order to properly show the progress dialog it must be executed on UIThread, while all the other work(service call) - in another. See the example here.
I've got an Android activity which grabs an RSS feed from a URL, and uses the SAX parser to stick each item from the XML into an array. This all works fine but, as expected, takes a bit of time, so I want to use AsyncActivity to do it in the background. My code is as follows:
class AddTask extends AsyncTask<Void, Item, Void> {
protected void onPreExecute() {
pDialog = ProgressDialog.show(MyActivity.this,"Please wait...", "Retrieving data ...", true);
}
protected Void doInBackground(Void... unused) {
items = parser.getItems();
for (Item it : items) {
publishProgress(it);
}
return(null);
}
protected void onProgressUpdate(Item... item) {
adapter.add(item[0]);
}
protected void onPostExecute(Void unused) {
pDialog.dismiss();
}
}
Which I call in onCreate() with
new AddTask().execute();
The line items = parser.getItems() works fine - items being the arraylist containing each item from the XML. The problem I'm facing is that on starting the activity, the ProgressDialog which i create in onPreExecute() isn't displayed until after the doInBackground() method has finished. i.e. I get a black screen, a long pause, then a completely populated list with the items in. Why is this happening? Why isn't the UI drawing, the ProgressDialog showing, the parser getting the items and incrementally adding them to the list, then the ProgressDialog dismissing?
I suspect something is blocking your UI thread after you execute the task. For example, I have seen folks do things like this:
MyTask myTask = new MyTask();
TaskParams params = new TaskParams();
myTask.execute(params);
myTask.get(5000, TimeUnit.MILLISECONDS);
The get invocation here is going to block the UI thread (which presumably is spinning off the task here...) which will prevent any UI related stuff in your task's onPreExecute() method until the task actually completes. Whoops! Hope this helps.
This works for me
#Override
protected void onPreExecute() {
dialog = new ProgressDialog(viewContacts.this);
dialog.setMessage(getString(R.string.please_wait_while_loading));
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
}
It is because you used AsyncTask.get() that blocks the UI thread "Waits if necessary for the computation to complete, and then retrieves its result.".
The right way to do it is to pass Activity instance to your AsyncTask by constructor, and finish whatever you want to do in AsyncTask.onPostExecution().
If you subclass the AsyncTask in your actual Activity, you can use the onPostExecute method to assign the result of the background work to a member of your calling class.
The result is passed as a parameter in this method, if specified as the third generic type.
This way, your UI Thread won't be blocked as mentioned above. You have to take care of any subsequent usage of the result outside the subclass though, as the background thread could still be running and your member wouldn't have the new value.