Progressbar update causing a black screen - android

I am trying to update the progressbar, but it is showing a black screen whenever I use progressbar. As soon as I comment the part of progressbar increment, my code works fine. Could someone please help me out with what am I doing wrong?
new Runnable() {
#Override
public void run() {
for(int i = 0; i < timeout; i++) {
handler.post(new Runnable() {
#Override
public void run() {
progressbar.incrementProgressBy(1);
}
});
try {
Thread.sleep(1000);
}
catch(Exception e) {}
}
}
}.run();

using thread you can do like this...
public void loadProgressbar()
{
progressBar.setMax(100);
progressBar.setProgress(0);
new Thread(new Runnable() {
#Override
public void run() {
for (progress = 0; progress <= 100; progress++) {
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progress);
}
});
try {
Thread.sleep(50);
} catch (Exception ex) {
}
}
}
}).start();
}

Just use this code on your main thread .
new CountDownTimer(3000, 300) {
#Override
public void onTick(long l) {
progress = progress + 10;
pb.setProgress(progress);
}
#Override
public void onFinish() {
pb.setProgress(100);
}
}.start();

Related

Android update ProgressBar with Handler

I would like to update the progressBar with Handler and for loop but without success.
Code:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(progress_k, 100);
}
}, 2000);
}
}
Question:
The progress bar increase immediately to the end value instead of progressively.
Why?
Try this:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
final int curr_progress_k = progress_k;
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
}
}, progress_k * 100); // adjust "100" value to adjust speed
}
}
Repeat a task with a time delay?
#inazaruk
private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
progressBar = (ProgressBar) findViewById(R.id.pb);
progressBar.setProgress(0);
mHandler = new Handler();
runnable.run();
}
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
updateProgress();
} catch (Exception ignored) {
} finally {
mHandler.postDelayed(runnable, progressInt);
}
}
};
private void updateProgress() {
progressInt += 1;
if (progressInt > 100) {
mHandler.removeCallbacks(runnable);
} else {
progressBar.setProgress(progressInt);
}
}
try this code:
Solution 1
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler();
class Task implements Runnable {
int start,end;
Task(int a,int b) { start = a; end = b;}
#Override
public void run() {
for (int i =start ; i <= end; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler1.post(new Runnable() {
#Override
public void run() {
progressBar.setProgress(value);
}
});
}
}
}
Thread t = new Thread(new Task(from, to)); //call it
t.start();
}
Solution 2: More Simple
If thread is too much to ask for this problem..
you can use the following solution to use a single Handler to update progressbar:
code
public class HandlerDemo extends Activity
{
ProgressBar bar;
Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
bar.incrementProgressBy(5);
}
};
boolean isRunning = false;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
}
public void onStart()
{
super.onStart();
bar.setProgress(0);
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 20 && isRunning; i++)
{
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch (Throwable t)
{
// just end the background thread
}
}
});
isRunning = true;
background.start();
}
public void onStop()
{
super.onStop();
isRunning = false;
}
}
Hope it helps..

Make elements in ImageView array visible using Thread

I have an array of ImageView with a number of invisible elements that I would like to make visible sequentially (i.e. make the first one visible, wait for half a second, make the next one visible, and so on) using either Thread or runOnUiThread.
I have tried the following:
Thread th = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
th.start();
and
runOnUiThread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(500);
im1.setVisibility(View.VISIBLE);
Thread.sleep(500);
im2.setVisibility(View.VISIBLE);
Thread.sleep(500);
im3.setVisibility(View.VISIBLE);
Thread.sleep(500);
im4.setVisibility(View.VISIBLE);
Thread.sleep(500);
im5.setVisibility(View.VISIBLE);
Thread.sleep(1000);
bottom.setVisibility(View.VISIBLE);
} catch (Exception e) {
Log.e("ERR", e.getMessage());
}
}
});
But I don't get the desired effect. What happens is that all images are made visible at the same time instead of one after the other.
How can I solve this?
int counter=1; //Global variable
private void visibleImageview()
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(counter==1)
{
im1.setVisibility(View.VISIBLE);
}
else if(counter==2)
{
im2.setVisibility(View.VISIBLE);
}
else if(counter==3)
{
im3.setVisibility(View.VISIBLE);
}
++counter;
if(couter<=3)
visibleImageview();
}
}, 3000);
}
Solved it. This is how I managed to do it:
Handler handler = new Handler();
im1.setVisibility(View.VISIBLE);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im2.setVisibility(View.VISIBLE);
}
});
}
},500);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im3.setVisibility(View.VISIBLE);
}
});
}
},1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},1500);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
im4.setVisibility(View.VISIBLE);
}
});
}
},2000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
bottom.setVisibility(View.VISIBLE);
}
});
}
},3000);

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);

start new activity on progressbar 100%

I use this Tutorial to create custom Progressbar and it works .
But I want to start new activity when progress bar go to 100% .
anyone can help to put start new activity code in correct place?
You can check if the progress has reached the maximum possible while you're setting it, like this:
#Override
public synchronized void setProgress(int progress) {
super.setProgress(progress);
// the setProgress super will not change the details of the progress bar
// anymore so we need to force an update to redraw the progress bar
invalidate();
if(progress >= getMax()) {
Intent intent....
}
}
You can call the onContinue function timer thread and set the intent to next activity and register the activity name in manifest file.
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progress);
mProgressBar = (ProgressBar)findViewById(R.id.adprogress_progressBar);
final Thread timerThread = new Thread() {
#Override
public void run() {
mbActive = true;
try {
int waited = 0;
while(mbActive && (waited < TIMER_RUNTIME)) {
sleep(200);
if(mbActive) {
waited += 200;
updateProgress(waited);
}
}
} catch(InterruptedException e) {
} finally {
onContinue();
}
}
};
timerThread.start();
}
#Override
public void onDestroy() {
super.onDestroy();
}
public void updateProgress(final int timePassed) {
if(null != mProgressBar) {
final int progress = mProgressBar.getMax() * timePassed / TIMER_RUNTIME;
mProgressBar.setProgress(progress);
}
}
public void onContinue() {
Intent intd=new Intent(this,MainActivity.class);
startActivity(intd);
}
Try this...
new Thread(new Runnable() {
public void run() {
while (progressStatus < 100) {
progressStatus += 5;
// Update the progress bar and display the current value in the text view
handler.post(new Runnable() {
public void run() {
progressBar.setProgress(progressStatus);
textView.setText("Loading "+progressStatus+"/"+progressBar.getMax());
}
});
try {
// Sleep for 200 milliseconds. Just to display the progress slowly
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
**Intent intent = new Intent(Main_Activity.this, Send_Email.class);
startActivity(intent);**
}
}).start();

Categories

Resources