I have a fragment with some buttons in it, when a button is clicked it should show a ProgressDialog, load an array of bitmaps and show it in the fragment in a gallery, dismiss ProgressDialog.
But the ProgressDialog doesn't show immediately, it take something like 1 or 2 seconds and it just blink at the moment when my gallery is show.
Im doing this after click:
try{
progress = ProgressDialog.show(activity, "", "Loading images", true);
//load images
//show gallery
}catch(){
//...
}finally{
handler.sendEmptyMessage(0);
}
My Handler at onCreate:
handler = new Handler() {
public void handleMessage(Message msg) {
progress.dismiss();
}
};
Im using Android 3.1
Logcat shows anything :(
03-09 13:17:32.310: D/DEBUG(5695): before show()
03-09 13:17:32.350: D/DEBUG(5695): after show()
You are loading the images on the main UI thread - you should do this in a background process as it may cause your UI to become unresponsive (and cause your ProgressDialog to show up at the wrong time).
You should look into using an AsyncTask to carry out loading of the images in the background.
Display the ProgressDialog in AsyncTask.onPreExecute, load images in AsyncTask.doInBackground and dismiss the dialog in AsyncTask.onPostExecute.
Documentation does not tell much about setIndeterminate(boolean), so I'm not sure. But I use this in my app, and it works:
ProgressDialog fDialog = new ProgressDialog(your-context);
fDialog.setMessage(your-message);
fDialog.setIndeterminate(true);
// fDialog.setCancelable(cancelable);
fDialog.show();
Could you try it?
Related
I already have idea on how to use a Progress Dialog when background action is being performed. But my question is how do I show a progress Dialog when I am dynamically inflating a huge layout.
Since I can't inflate a view in another Thread, I am using the main UI thread. Due to this my progress dialog is not getting priority and it doesn't show up. My UI hangs for several seconds until it gets loaded fully. I tried several approcahes but none seems to work.
progress.show(context,"","inflating UI...");
setNewContent(R.layout.my_profile,R.id.my_profile_menu_button,R.id.my_profile_all_elements_layout);
populateProfileList(); //Dynamic nested layouts being inflated.
I am basically looking for dynamic layout changes based on user actions. So I dont have any other way other than creating dynamic views. Can anyone suggest me a helpful idea.
I had an similar problem with ui-thread. I wanted to add much views to my layout at runtime, I wanted to show a ProgressDialog to inform the user, that it could take a while. I had tried it with AsyncTask, but the only chance to do this, was to implement my ui-methods into the onPostExecute-Method of AsyncTask. But this just freezes the ProgressDialog, because ProgressDialog even works on ui-thread. I found a solution, maybe not the best, but it works. Before starting ui-action, I show a Toast. Then, I created a Thread with a handler and delayed the execution. After ui-action was done, I showed a new Toast. Delay the thread, gives the Toast enough time to get showed:
Toast.makeText(ActivityContext.this,
R.string.start_message,
Toast.LENGTH_SHORT).show();
final Handler uiHandler = new Handler();
final Thread uiThread = new Thread(new Runnable() {
#Override
public void run() {
uiHandler.postDelayed(new Runnable() {
#Override
public void run() {
yourUIAction();
Toast.makeText(
ActivityContext.this,
R.string.finish_message,
Toast.LENGTH_SHORT).show();
}
}, 100);
}
});
uiThread.start();
You need to use a handler to be able to talk to the UI thread
//in some thread
uiHandler.sendEmptyMessage(1);
...
Handler uiHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
if(msg.what == 1) {
//do somestuff
}
}
};
Using Async Task is much better idea to me, when you have to do something in background, while progress dialog shows up on screen and then when background task completes, update UI... for reference please follow the link...
http://labs.makemachine.net/2010/05/android-asynctask-example/
hope this helps...:-)
There are a number of questions involving the lack of ability to dismiss a ProgressDialog, but none of them seem to cover my situation.
I have a Thread that runs a Runnable object that, when it completes, sends a message to a Handler object which I'm certain is sitting on the same thread as the ProgressDialog. The handler does this:
if(progressDialog != null){
Log.w(TAG, "Progress dialog is dismissed");
progressDialog.dismiss();
}else{
Log.w(TAG, "Progress dialog is null");
}
I've done this a million times before, and it's worked. The ProgressDialog goes away. But, in one particular instance, it doesn't.
In this particular case, a ProgressDialog (we'll call uploadChangesDialog) is showing, then a particular Handler (uploadChangesHandler) is called. After dismissing the uploadChangesDialog, it does a check that, if true, starts a different ProgressDialog (refreshViewDialog) and a Runnable (refreshViewRunnable) in a Thread. However, when it's Handler is called (refreshViewHandler), it can't close the dialog. But it does log Progress dialog is dismissed.
This is particularly strange, because the refreshViewsRunnable is run when the Activity is started, too, but it can get rid of the dialog then, just fine. The progressDialog variable above is the only one of it's kind, which is supposed to hold whatever ProgressDialog is currently showing.
I've done this with AlertDialogs before, but they know how to close themselves, so if I'm doing something wrong, then I wouldn't have noticed.
In the onCreateDialog() method:
case DIALOG_REFRESH_VIEW:
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading details...");
progressDialog.setCancelable(false);
return progressDialog;
Copied for each instance, with a different message. I did change it to all dialogs pointing to the same code, and setting the message in onPrepareDialog(), but that didn't change any behaviour.
In the UploadChangesRunnable:
public void run(){
int result = 0;
if(uploadChanges()){
result = 1;
}
uploadChangesHandler.sendEmptyMessage(result);
}
And then in uploadChangesHandler:
public void handleMessage(Message msg){
if(progressDialog != null){
progressDialog.dismiss();
}
if(msg.what == 0){
showDialog(DIALOG_UPLOAD_CHANGES_FAILED); //This is an AlertDialog
}else{
//All this does is showDialog(DIALOG_REFRESH_VIEW) then run the thread.
//This method is in the enclosing Activity class.
refreshViewInThread();
}
}
Finally, the refreshViewInThread method:
private void refreshViewInThread(){
showDialog(DIALOG_REFRESH_VIEW);
Thread thread = new Thread(new RefreshViewRunnable(refreshViewHandler));
thread.start();
}
And the RefreshViewRunnable looks remarkably similar to the UploadChangesRunnable.
There must be some special case that makes me lose the link to my progressDialog, and the dialog that I'm dismissing is likely not the dialog that is showing, but I can't think of how that could be. Any ideas?
my guess is that this is a context/handler issue. I had a similar problem, and decided to use interfaces/callbacks instead of Handler, and it fixed my problem.
However, you could try the following, where you see this: >> But it does log Progress dialog is dismissed.<< , add:
MyActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
progressDialog.dismiss();
}
});
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.
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 click the grid item, i want to show a progressbar between the time of next Activity shown. then the second activity has a custom listview. there also i want to show a progressbar. how to do that?
I'm just learning myself and I haven't had a chance to inspect the source, but I know that the RedditIsFun app (for Reddit, of course) does this in-between loading the next links, and loading comments. Check out the source.
You should try this,
First Create the ProgressDilog in Activity. . .
private ProgressDialog progressDialog;
Now, Set the ProgressDialog in the Any action where u want to Take Some time
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use Thread to Handle the Progressbar
new Thread()
{
public void run()
{
try
{
sleep(1500);
// Now Do some Task whatever u want to do
}
catch(Exception e)
{
Log.e("tag",e.getMessage());
}
}.start();
Thats it. Hope this will help you.