I have a problem of showing the progressDialog on android. It did shows up to the screen but it took few seconds before it really show the dialog.
This is the code i did to show the dialog
Handler saveHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
GallerySaveActivity.this.Submit(progress);
Button btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setEnabled(true);
}
};
progress.showDialog(saveHandler, "", "Accessing Facebook ...");
Thread progress_thread = new Thread(progress);
progress_thread.start();
Do I have to do any extra work on the Thread object in order to show the dialog instantly without any delay.
Consider using an AsyncTask: show your dialog in onPreExecute(), and do your background tasks in doInBackground().
Related
After the splash screen, it takes about 6 sec to load onCreate contents in the Main activity. So I want to show a progress dialog while loading and here's what I did:
import ...
private ProgressDialog mainProgress;
public void onCreate(Bundle davedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mProgress = new ProgressDialog (Main.this);
mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgress.setMessage("Loading... please wait");
mProgress.setIndeterminate(false);
mProgress.setMax(100);
mProgress.setProgress(0);
mProgress.show();
---some code---
mProgress.setProgress(50);
---some code---
mProgress.setProgress(100);
mProgress.dismiss();
}
and it doesn't work... the screen stays black for 5-6 sec and then load the main layout. I dont know which part I did wrong :*(
You need to do start your busy code in an other thread (than the UI thread).
In your activity create the ProgressDialog and close it in the thread
mProgress = new ProgressDialog (Main.this);
Runnable runnable = new Runnable() {
#Override
public void run() {
// code that needs 6 seconds for execution
// after finishing, close the progress bar
mProgress.dismiss();
}
};
new Thread(runnable).start();
It's because you're doing everything on the UI thread, thus blocking it.
You should do all your heavy-lifting in a separate thread and update the progress dialog through that
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
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();
-->I am new to Android And i want to show two progress dialog one after another??
-->First i want to show when my image is load from internet, when this process is done i have set A button on that Remote image.
-->When i click that button i want Dialog for second time..(on clicking button i have set video streaming code.. before video is start i want to close that Dialog..)
Any Help????
Thanks...
You can create 2 threads. First for image when it is loaded then call another thread of video.
Make two runnable actions and two handlers to handle that
public void onCreate(Bundle savedInstanceState) {
ProgressDialog pd = ProgressDialog.show(this, "","Please Wait", true, false);
Thread th = new Thread(setImage);
th.start();
}
public Runnable setImage = new Runnable() {
public void run() {
//your code
handler.sendEmptyMessage(0);
}
};
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (pd != null)
pd.dismiss();
}
};
On Android it's best to use AsyncTask to execute tasks in the background while still updating UI:
Extend the AsyncTask
Start the progress dialog in onPreExecute()
Define the background task in doInBackground(Params...)
Define updating of the progress dialog in onProgressUpdate(Progress...)
Update dialog by calling publishProgress() from doInBackground()
Start a new Dialog #2 in onPostExecute().
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.
This is what I got:
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
(...)
ProgressDialog pd = new ProgressDialog(MyApp.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
// now fetch the results
(...long time calculations here...)
// remove progress dialog
pd.dismiss();
I've also tried adding pd.show(); and messed around with the parameter in new ProgressDialog resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.
I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm() in MyApp.
Any hint is appreciated,
you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
here you can see an example:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
#Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
and here the thread:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
#Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
I am giving you a solution for it,
try this...
First define the Progress Dialog in the Activity before onCreate() method
private ProgressDialog progressDialog;
Now in the onCreate method you might have the Any button click on which you will change the Activity on any action. Just set the Progress Bar there.
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use thread to handle the Progress Bar to Display and hide
new Thread()
{
public void run()
{
try
{
sleep(1500);
// do the background process or any work that takes time to see progress dialog
}
catch (Exception e)
{
Log.e("tag",e.getMessage());
}
// dismiss the progress dialog
progressDialog.dismiss();
}
}.start();
That is all!
Progress Dialog doesn't show because you have to use a separated thread. The best practices in Android is to use AsyncTask ( highly recommended ).
See also this answer.
This is also possible by using AsyncTask. This class creates a thread for you. You should subclass it and fill in the doInBackground(...) method.