Spinner Progressbar in android - 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);

Related

Try to create stopper in my application

i created running app and i trying to create stopper that starting when user press start button.
but when i using thread the application return black screen
,this is my onCreate method i added thread
Thread t = new Thread() {
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
}
};
t.start();
the SetTextOnView method contain
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
while(true)
{
timedisp.setText(String.valueOf(total));
}
}
the problem start when i add the while statment
why is that?
Do the loop in your thread to not block the ui thread + add some delay to not block the ui thread :
Thread t = new Thread() {
public void run() {
while (true) {
runOnUiThread(new Runnable() {
#Override
public void run() {
SetTextOnView();
}
});
Thread.sleep(1000);
}
}
};
And your SetTextOnView :
public void SetTextOnView()
{
TextView timedisp = (TextView) findViewById(R.id.stopperdisplay);
timedisp.setText(String.valueOf(total));
}

how to stop progressbar which run on runnable thread in android

I am developing a quiz application in which I am using runnable thread for seekbar functionality. seekbar shows how much time is remaining. Now I want to stop the seekbar when a answer button is click.
here is my code for runnable thread.
new Thread(new Runnable() {
public void run() {
while (seekbarStatus < 100) {
seekbarStatus = LoadingStatus();
// sleep 1 second to show the progress
try {
Thread.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
// Update the progress bar
progressBarHandler.post(new Runnable() {
public void run() {
seekbar.setProgress(seekbarStatus);
}
});
}
if (seekbarStatus >= 100) {
// sleep for 2 seconds, so that you can see the 100% of file download
runOnUiThread(new Runnable() {
#Override
public void run() {
if(!(Ans_1.getTag()=="clicked" ||Ans_2.getTag()=="clicked" || Ans_3.getTag()=="clicked" ))
{
Ans_1.setEnabled(false);
Ans_2.setEnabled(false);
Ans_3.setEnabled(false);
Ans_1.setBackgroundColor(Color.GREEN);
points.setText("0 Punkt");
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
showDialogView();
}
}, SPLASH_DISPLAY_LENGHT);
}
}
public void showDialogView() {
// TODO Auto-generated method stub
dialog.show();
}
});
}
}
}).start();
please help me. Any help would be appreciated.
I am not getting how to solve it.
Thanks in advance.
Just add an instance variable:
private boolean isCancelled = false;
Then in the button onClick:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
isCancelled = true;
}
});
Then change
while (seekbarStatus < 100) {
to
while (seekbarStatus < 100 && !isCancelled) {

why thread creation is failed?

I want to turn on/off the flash light in infinite loop, so when it turned on it should wait for 5 seconds and then turned off then wait 5 seconds to turned on again, and so on...
how I can do that?
here is my code:
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// num = Integer.parseInt(n.getText().toString());
while(bl){
if(camera == null){
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOn();
}
});
}
}).start();
}
else{
new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
#Override
public void run() {
turnOff();
}
});
}
}).start();
}
}
}
});
I would recommend not using Threads in order to achieve this. Why not use the Runnable class and post it with a delay via a Handler? For example:
Handler handler = new Handler(); // make this a member variable of your class
boolean isOn = false; // make this a member variable of your class
final Runnable flashRunnable = new Runnable() {
#Override
public void run() {
if (isOn) {
turnOff();
isOn = false;
} else {
turnOn();
isOn = true;
}
handler.postDelayed(flashRunnable, 5000);
}
};
handler.postDelayed(flashRunnable, 5000);
If you need to run the code inside the Runnable on the UI thread, you even call postDelayed on a View instead of creating a Handler
Try something like so, using Executors instead of (ugly) Thread.sleep():
boolean cameraOn = true
final Runnable runnable = new Runnable() {
#Override
public void run() {
// your logic here:
// if (cameraOn) ...
// else ...
// cameraOn = !cameraOn
}
};
Executors.newScheduledThreadPool(1).schedule(new Runnable() {
#Override
public void run() {
runnable.run();
}
}, 5, TimeUnit.SECONDS);

Why do I need a Handler object in this example?

public class ProgressBarTest extends Activity {
private int progress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress_bar);
final Handler handler = new Handler();
progress = 0;
final ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
new Thread(new Runnable() {
public void run() {
while (progress < 10) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
}
handler.post(new Runnable() {
#Override
public void run() {
pb.setVisibility(View.GONE);
}
});
}
}).start();
}
}
Why can't I just put the pb.setVisibility(View.GONE) in the first Runnable inner class? Like this: The program crashes if I write it this way.
new Thread(new Runnable() {
public void run() {
while (progress < 10) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
progress++;
pb.setVisibility(View.GONE);
}
}
}
The program crashes when the setVisibility statement is executed.
You cannot update ui from a thread. Ui should be updated on the ui thread.
In the second one you are setting the visibility of progressbar inside the threads runs method. Hence it crashes. So you use handler to set the visibility of progress bar in the the first
To know more about handlers.
http://developer.android.com/reference/android/os/Handler.html

Updating UI using a Handler freezes my app

I am trying to make a clock, using a TextView :)
Someone here told me that I couldn't use normal threads to change the UI, but Handler or AsyncTask. I managed to get it working a few days ago, but was not a consistent thread.
Now what I want is a consistent thread that is always changing the text of my Textview. I tried using this, but didn't work, any help?
private void startClock() {
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
while (true) {
final long millis = System.currentTimeMillis() - MainActivity.startedAt;
clock.setText("" + millis);
runOnUiThread (new Runnable() {
#Override
public void run() {
clock.setText("" + millis);
}
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}, 2000);
}
you should get rid of:
while(true) {
....
sleep(1000);
...
}
because this get your thread stuck forever. your program should work like this:
private Handler mHandler = new Handler();
#Override
public void onResume() {
super.onResume();
mHandler.removeCallbacks(mUpdateClockTask);
mHandler.postDelayed(mUpdateCLockTask, 100);
}
#Override
public void onPause() {
super.onPause();
mHandler.removeCallbacks(mUpdateClockTask);
}
private Runnable mUpdateClockTask = new Runnable() {
public void run() {
updateClock();
mHandler.postDelayed(mUpdateClockTask, 2000);
}
};
and inside updateClock() you do all your UI updates.
Look here for an example https://stackoverflow.com/a/11140429/808940
Also note that you have a duplicate line in your code:
clock.setText(""+millis);
It appears both in the runOnUiThread and in the main handler, it should only appear in the runOnUiThread runnable

Categories

Resources