Android ProgressDialog stops spinning - android

I have created a ProgressDialog in my android application. But the problem I am having is during the point where it is actually doing the work it stops spinning the wheel. Here is my code. How can I make it so it continually spins the wheel while my other work is going on?
button5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("Button5");
//Handler to make the please wait message
final ProgressDialog myProgressDialog = ProgressDialog.show(
FoodSubstitutesActivity.this, "Please wait...",
"Getting most recent updates...", true);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
//DO STUFF - STOPS SPINNING WHEEL UNTIL THIS PART IS COMPLETE.
myProgressDialog.dismiss();
}
}, 500);
}
});

Why dont you try doing it this way?
final ProgressDialog dialog = ProgressDialog.show(this, "Title",
"Message", true);
final Handler handler = new Handler() {
public void handleMessage(Message msg) {
dialog.dismiss();
}
};
Thread checkUpdate = new Thread() {
public void run() {
//
// YOUR LONG CALCULATION (OR OTHER) GOES HERE
//
handler.sendEmptyMessage(0);
}
};
checkUpdate.start();
taken from: http://www.tutorials-android.com/learn/How_to_display_a_progress_dialog_while_computing.rhtml

use this code this may help you,
// TODO Auto-generated method stub
myProgressDialog = ProgressDialog.show(MainActivity.this,
"", "Please wait....");
myProgressDialog
.setProgressStyle(ProgressDialog.STYLE_SPINNER);
new Thread() {
public void run() {
try {
Thread.sleep(1000);
} catch (Exception e) {
}
runOnUiThread(new Runnable() {
#Override
public void run() {
// do your action...........
finish();
}
});
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
Thanks...

Related

ProgressDialog is not showing in android

I know that is easy to do it, but I've tried to take some examples from here and is not showing the progressDialog. That I want to do is show a ProgressDialog when click the button and finish when the task is finished:
Thanks in advance!
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
}
});
};
}).start();
progress.dismiss();
}
});
Your ProgressDialog is showing but its dismissed right after the show. Put your dismiss inside the run method of your runnable.
This way:
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
progress.dismiss();
}
});
};
}).start();
}
});
a thread is an independent process which runs in background your progress bar is showing and dismissing immediately, call progress.dismiss(); in your run() after your onPhotoTaken()
in below code you dismiss the dialog as it start also running thread so dismissing dialog can't wait for task which is inside thread.
buttonStartOCR.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progress = ProgressDialog.show(SimpleAndroidORCActivity.this, "Processing", "Please wait...", true);
new Thread(new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
onPhotoTaken();
progress.dismiss();
}
});
};
}).start();
}
});
onPhotoTaken() :- should return any value so that we can dismiss dialog.
and dismiss dialog in main thraed

Spinner Progressbar in android

I want to implement ProgressBar in Android and when I execute the program, Progressbar should show for up to 2 seconds. I can't get it to work properly but I can't figure out why.
public void myThread(){
Thread th=new Thread(){
#Override
public void run(){
try
{
while(mRunning)
{
Thread.sleep(10L);//10s wait
YourCurrentActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
//DISMISS PROGRESS BAR HERE
mRunning=false;
}
});
}
}catch (InterruptedException e) {
// TODO: handle exception
}
}
};
th.start();
}
I have tried this but it does not giving me output as i want.
That what handlers are for in Android.
Example:
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
// cancel or dismiss your progressbar
}
}, 2000);

Android ProgressDialog doesn't show in onCreate using Thread

I want to load some contacts when an activity is created. Because this is a long running operation I want to notify the user through a ProgressDialog. A request for this app is to not use AsyncTasks so I'm using threads but the progress is not showing. This is my onCreate method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
handler = new Handler();
mActionBar = getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
progress = new ProgressDialog(this);
progress.setMessage("Loading Contacts ");
progress.setIndeterminate(true);
progress.show();
Thread t = new Thread(new Runnable() {
#Override
public void run() {
contacts = ContactsUtils.getContacts(getContentResolver());
handler.post(new Runnable() {
public void run() {
progress.dismiss();
// UpdateDisplay();
};
});
}
});
t.start();
try {
t.join();
} catch (InterruptedException e) {
Log.e("Interrupted Exception:", e.getLocalizedMessage());
}
// other methods
}
Am I missing something? Thanks
SOLVED: The rest of the code from onCreate, after the join, should be moved to a Handler, and join should be removed
You are missig
progress.show()
plus why you are using threads don't you think async tasks will do this task with beauty?
The issue you don't see your progress dialog is because
try {
t.join();
} catch (InterruptedException e) {
Log.e("Interrupted Exception:", e.getLocalizedMessage());
}
this hold main thread till your task is done, just comment it
Try This Code:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.launching);
final ProgressDialog myPd_ring=ProgressDialog.show(Launching.this, "", "Loading please wait..", true);
myPd_ring.setCancelable(true);
new Thread(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
try
{
Thread.sleep(2000);
}catch(Exception e){}
Intent intt=new Intent();
intt.setClass(Launching.this, MainController.class);
startActivity(intt);
finish();
myPd_ring.dismiss();
}
}).start();
}
Whenever you're trying to do something with the UI from a background thread, either use a Handler or call it within runOnUiThread(Runnable())
try something like this:
private ProgressDialog pDialog; // global
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
color_list = (ListView) this.findViewById(R.id.color_list);
imageView1 = (ImageView)findViewById(R.id.icon);
pDialog = new ProgressDialog(Launcher.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
showProgressDialog();
Thread t = new Thread(new Runnable() {
#Override
public void run() {
contacts = ContactsUtils.getContacts(getContentResolver());
handler.post(new Runnable() {
public void run() {
hideProgressDialog();
};
});
}
});
}
private void showProgressDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideProgressDialog() {
if (pDialog.isShowing())
pDialog.hide();
}

Access a custom class from a new thread in a ProgressDialog on Android

I have created a ProgressDialog in android and it works when I do a simple example.
For example, this works.
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Getting updates...", true);
new Thread()
{
public void run()
{
try
{
// Do some Fake-Work
sleep(5000);
}
catch (Exception e)
{
}
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
But once I add in a reference to my custom class, it just stops running this new thread.
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
// Display an indeterminate Progress-Dialog
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Getting Updates...", true);
new Thread()
{
public void run()
{
try
{
HealthySubObject hsObject = new HealthySubObject();
// Do some more work with my hsObject - nothing happens after this point.
sleep(5000);
}
catch (Exception e)
{
}
// Dismiss the Dialog
myProgressDialog.dismiss();
}
}.start();
}
});
What happens is that as soon as I click this button, the progress dialog flashes up on the screen real quick and then disappears. But if you look at my code, it should wait 5 seconds before disappearing. I have put debug statements before and after the reference to my custom class and I can see the statements before but not the ones after. Does anyone have any idea why that is happening? As long as my class is public I should be able to call it from a new thread, right?
I am still pretty new to android and this is my first adventure into multi-threaded android apps. Any help would be much appreciated.
SOLUTION
Thanks for your help everyone. It is working now.
button1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// Perform action on click
System.out.println("Progess Bar");
//ProgressDialog dialog = ProgressDialog.show(AndroidTestApplicationActivity.this, "", "Loading. Please wait...", true);
// Display an indeterminate Progress-Dialog
final ProgressDialog myProgressDialog = ProgressDialog.show(AndroidTestApplicationActivity.this,
"Please wait...", "Doing Extreme Calculations...", true);
Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
#Override
public void run()
{
HealthySubObject hsObject = new HealthySubObject();
ArrayList<HashMap<String, String>> onlineDB = hsObject.jsonToArray();
//
// more stuff goes here.
//
//
myProgressDialog.dismiss();
}
}, 1500);
}
});
I would really recommend to use Handler instead of Thread. Using the Thread.sleep method is actually discouraged. Something like this is much better:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
HealthySubObject hsObject = new HealthySubObject();
myProgressDialog.dismiss();
}
}, 5000);
The problem is that you need to be on the UI thread to do modify the UI, and inside the run() method of your Thread you are in a "background" thread. Try using a handler inside your thread when you need to access the UI thread.

Android: Thread with progressBar and setcurrentTab

final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "",
"Loading. Please wait...", true);
Thread ProgressThread = new Thread() {
#Override
public void run() {
try {
sleep(3000);
Pdialog.dismiss();
} catch(InterruptedException e) {
// do nothing
} finally {
}
}
};
ProgressThread.start();
TabHost1 TabHost1Object2 = new TabHost1();
TabHost1Object2.tabHost.setCurrentTab(2);
The problem I have with this thread is that it sets the current tab before the progress dialog starts. What have i done wrong ?
I want the dialog to run and dismiss, and after thread is done set tab.
use AsyncTask for this
some hints:
public class BackgroundAsyncTask extends AsyncTask<Void, Integer, Void> {
int myProgress;
#Override
protected void onPostExecute(Void result) {
TabHost1 tab = new TabHost1();
tab.tabHost.setCurrentTab(2);
progressBar.dismiss();
}
#Override
protected Void doInBackground(Void... params) {
while(myProgress<100){
myProgress++;
publishProgress(myProgress);
SystemClock.sleep(100);
}
return null;
}
#Override
protected void onProgressUpdate(Integer p) {
progressBar.setProgress(p);
}
}
The thing is that,you are starting a thread which will not affect your main UI. So what eventually happens is that, your thread will run separately which will now allow the next lines of your code to be executed. So in your case,
TabHost1 TabHost1Object2 = new TabHost1();
TabHost1Object2.tabHost.setCurrentTab(2);
these lines will be executed irrespective to your thread which is also getting executed simultaneously. So what you can do here is you can either go for AsyncTask or create handlers to handle this part of your code. You have to change your code like this.
Do this in your onCreate()
Handler handler;
handler = new Handler() {
#Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
Pdialog.dismiss();
TabHost1 TabHost1Object2 = new TabHost1();
TabHost1Object2.tabHost.setCurrentTab(2);
}
};
And now in your thread,call the handler like this,
final ProgressDialog Pdialog = ProgressDialog.show(SpinnerClass.this, "",
"Loading. Please wait...", true);
Thread ProgressThread = new Thread() {
#Override
public void run() {
try {
sleep(3000);
} catch(InterruptedException e) {
// do nothing
} finally {
handler.sendEmptyMessage(0);
}
}
};
this will allow your tabhost to wait until the thread gets executed and will come into view after thread finishes execution.

Categories

Resources