In my android application, dialog's showing up takes a time. At that time I want to show progress dialog. I learned that the progress dialog should be executed in thread but when I use a thread it gives an error.
I created progress dialog in oncreate method and tried to show in my button's onclick method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
mProgressDialog = new ProgressDialog(context, R.style.StyledDialog);
mProgressDialog.setCanceledOnTouchOutside(false);
Drawable drawable = context.getResources().getDrawable(R.drawable.progress_dialog);
mProgressDialog.setProgressDrawable(drawable);
}
Following code is my button's onclick method which is defined in xml file like : android:onClick="refList"
public void refList(View v) {
Thread t = new Thread(new Runnable() {
#Override
public void run() {
mProgressDialog.show();
}
});
t.start();
if(!refListDialog.isShowing()) {
refListDialog.show();
t.interrupt();
}
}
This is the exception:
FATAL EXCEPTION: Thread-27305 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:3052)
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:3321)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:294)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
at android.app.Dialog.show(Dialog.java:277)
at gcm.b4deploy.com.hesapozeti.MainActivity$2.run(MainActivity.java:196)
at java.lang.Thread.run(Thread.java:856)
I am really stuck and waiting ideas. Thanks in advance.
I really don't get the idea why you put the progressDialog inside a thread.
yourProgress = new ProgressDialog(this);
yourProgress.setTitle("Title");
yourProgress.setMessage("wait for a while"); yourProgress.getProcess();
yourProgress.show();
Put this code first in your function in the onClick method. It will execute sequentially.And you can make conditional statement, and if done, call yourProgress.dismiss();
- add your statement above the progressDialog.setTitle. I suggest you declare the progress dialog outside this method, so that you can dismiss that one in the other method which return true. Just remove those runnable thing.
Try like this
runOnUiThread(new Runnable() {
public void run() {
mProgressDialog.show();
}
});
Related
I have a thrad that calls a function that populates a list in an activity, I would put a progressDialog when the function is running, I wonder what I do to change the progress dialog is invoked when the activity is called, follows current code :
new Thread(){
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
loadList();
}
});
}
}.start();
I think you would be better off using an AsyncTask, easy to attach a progress dialog to this.
Here's an example: http://droidapp.co.uk/?p=177
Create and start your progress dialog in the UI-Thread, then start your Thread. Create a Runnable object which dissmiss() your progress. Invoke this Runnable when you finished populating your list with runOnUiThread.
How can I show a progressDialog during the start up of an application. I have shown a progressDialog in the oncreate method and its not showing when launching the application.
I have gone through this:
ProgressDialog not showing until after function finishes
I have tried the solution explained for the above question. But its not working perfectly.
Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dash);
progressDialog = ProgressDialog.show(this, "", "Loading...");
//Run background UI thread
Thread laucherThread = new Thread(new Runnable() {
public void run() {
Looper.prepare(); //I had to include this to prevent force close error
startService(new Intent(DroidChirp.this,ChirpService.class));
doBindService();
Log.e(MY_DEBUG_TAG, "Prepairing to close the dialog");
runOnUiThread(new Runnable() {
public void run() {
progressDialog.dismiss();
}
});
Log.e(MY_DEBUG_TAG, "Closed the dialog");
}
});
laucherThread.start();
}
What I need to do is:
Show a progressDialog until all the initial setup is finished
Issues I am facing :
ProgressDialog is not showing at the start up.
There is a delay when starting the application(Showing blank for sometime).
ProgressDialog appears just before finishing the initial setup.
Can anyone suggest me how can I establish this feature. Its a tablelayout with list view for each tab.
If you want to show a progress dialog, while fetching data or something like this, you need to use AsyncTask with ProgressDialog bounded. See an example here.
I am using a simple progressDialog that running ok but the the wheel dose not progress:
//Progress Dialog
final ProgressDialog dialog = ProgressDialog.show(TravelPharm.this, "Searching","Please wait ...", true);
((ProgressDialog) dialog)
.setProgressStyle(ProgressDialog.BUTTON_NEUTRAL);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
what i am missing??
Create your progress dialog like so:
final ProgressDialog progress = new ProgressDialog(context);
add some text/icon to it:
progress.setTitle("Loading");
progress.setMessage("Loading, please wait");
progress.setIcon(R.drawable.icon);
Show it:
progress.show();
I think you should pass ProgressDialog.STYLE_SPINNER to ProgressDialog.setProgressStyle() method.
final ProgressDialog dialog = ProgressDialog.show(TravelPharm.this, "Searching","Please wait ...", true);
The way you are creating the ProgressDialog is correct - if the spinner isn't spinning then something is blocking your UI thread.
Out of interest, why are you using TravelPharm.this for the context instead of this? I'm not sure it's the cause of your problem, I'm just wondering why.
I am guessing that you are launching a time intensive task from a dialog and then trapping the thread exit in your handler where you are trying to dismiss the dialog. If possible, consider simply sending an empty message when the dialog is done. Then in the handler create a new AsyncTask as:
private class MyAsynch extends AsyncTask<String, Void, String>{
protected void onPreExecute() {
resetProgress();
progress.show();
}
#Override
protected String doInBackground(String...strings) { // <== DO NOT TOUCH THE UI VIEW HERE
// TODO Auto-generated method stub
doNonUIStuff();
return someString; // <== return value String result is sent to onPostExecute
}
protected void onPostExecute(String result){
progress.dismiss();
doSomethingWithString(result); // you could launch results dialog here
}
};
protected void onPause() {
super.onPause();
if (asynch != null) {asynch.cancel(true);}
if (progress != null){progress.cancel();}
}
private void resetProgress() { // avoid frozen progress dialog on soft kill
if (progress != null && progress.isShowing()){
progress.cancel();
}
progress= new ProgressDialog(this);
progress.setIndeterminate(true);
progress.setMessage("I am thinking.");
}
You could return any type in onPostExecute, in this example I am returning a string. Another approach would be to launch a second Activity as a "dialog" using startActivityForResult create the AsycnTask in onActivityResult.
In other words, gather the data in a dialog or second Activity, then in the first activity show a progress dialog in onPreExecute, do the time intensive task in the background, and cancel the progress dialog in onPostExecute.
I have seen the frozen spinning ball, thus the call to resetProgress().
-->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.