AsyncTask with a ProgressDialog and Progress Bar - android

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

Related

How to show ProgressDialog while loading UI items?

I have a listview and I'm loading dynamically items into it.
The problem is when I'm activating that activity. It takes too long, so I have to show some progress dialog while loading the UI.
I really don't know how to do it, since doInBackground (If using Async task) doesn't allow the developer to mess with the UI.
What should I do ?
Here is my UI load code :
LeagueTableRow leagueTableRow_data[] = new LeagueTableRow[]
{
new LeagueTableRow(1,R.drawable.logo,"aaa",12,9,3,14),
new LeagueTableRow(3,R.drawable.logo,"aaa",12,9,3,14),
new LeagueTableRow(4,R.drawable.logo,"aaa",12,9,3,14),
new LeagueTableRow(5,R.drawable.logo,"aaa",12,9,3,14),
new LeagueTableRow(2,R.drawable.logo,"aaa",12,9,3,14)
};
LeagueTableRowAdapter adapter = new LeagueTableRowAdapter(context,
R.layout.leaguetablecontent, leagueTableRow_data);
listViewLeagueTable = (ListView)findViewById(R.id.listViewLeagueTable);
View header = (View)getLayoutInflater().inflate(R.layout.leaguetableheader, null);
listViewLeagueTable.addHeaderView(header);
listViewLeagueTable.setAdapter(adapter);
That can be achieved with the help of AsyncTask (an intelligent backround thread) and ProgressDialog
A ProgressDialog with indeterminate state would be raised when the AsyncTask starts, and the dialog is would be dismissed once the task is finished .
Example code
What the adapter does in this example is not important, more important to understand that you need to use AsyncTask to display a dialog for the progress.
private class PrepareAdapter1 extends AsyncTask<Void,Void,ContactsListCursorAdapter > {
ProgressDialog dialog;
#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();
}
/* (non-Javadoc)
* #see android.os.AsyncTask#doInBackground(Params[])
*/
#Override
protected ContactsListCursorAdapter doInBackground(Void... params) {
cur1 = objItem.getContacts();
startManagingCursor(cur1);
adapter1 = new ContactsListCursorAdapter (viewContacts.this,
R.layout.contact_for_listitem, cur1, new String[] {}, new int[] {});
return adapter1;
}
protected void onPostExecute(ContactsListCursorAdapter result) {
list.setAdapter(result);
dialog.dismiss();
}
}
If you set the emptyview using listView.setEmptyView . The view will show in the place of list until you do a setadapter on the list. So As soon as you do setAdapter the emptyview will disappear. You can pass Progressbar to the setEmptyview in your case to show the progressbar.
Edit:
Use Setadapter in OnPostExecute of the Asynctask.
OnPostExecute, OnPreExecute, OnProgressUpdate, OnCancelled all run on UI thread. If you still insist on ProgressDialog, then create dialog in OnPreExecute and dismiss it in OnpostExecute and OnCancelled of the Asynctask
Please use AsyncTask if loading of list is taking too much time.Place a progress bar on the top of list view use that in AsyncTask.Hope this will help you

Why ProgressDialog is not always turning in my android application?

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

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.

In Android, usege of ProgressDialog.show() and ProgressDialog.hide() problem during fetching data from the internet

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.

Categories

Resources