Delay handler inside a loop in android [duplicate] - android

This question already has answers here:
How to delay a loop in android without using thread.sleep?
(4 answers)
Closed 10 months ago.
I am trying to insert the handler to the loop but it makes it happen only once.
for(int i = 0;i<4;i++) {
btn1.setBackgroundColor(Color.RED);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000);
}

If you want the button to change between red and green with a delay of 2 seconds you can try something like this:
for(int i = 0;i<4;i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.RED);
}
}, 2000*2*i);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000*(2*i+1));
}
When using
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 2000)
you set the delay to a fixed value.
to make the button blink the delay has to be dynamic and changed every loop iteration.

Related

Handler postDelayed - print every second something

I want to print the current second using a handler. I record a video for exactly 10 seconds and want to set the text of a TextView every second.
Recording 10 seconds works like that:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
}, 11000); // don't know why 11000 but it only works this way
After the 10 seconds the method stopRecordingVideo() gets executed. So how can I change the text every second of the TextView?
Working answer:
int t = 0;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
t++;
textView.setText(getString(R.string.formatted_time, t));
if(t<10) {
handler.postDelayed(this, 1000);
}
}
}, 1000);
Where formatted_time is something like that:
<string android:name="formatted_time">%d seconds</string>
To print text every second, you can use CountDownTimer. But if you want to achieve this with try below code:
void startTime(){
//Post handler after 1 second.
handler.postDelayed(runnable, 1000);
}
int totalDelay=0;
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
totalDelay++;
if(totalDelay<=10){
//If total time is less then 10 second execute handler again after 1 second
handler.postDelayed(runnable, 1000);
}
textView.setText(totalDelay+" Second");
}
};
Try this, basically do the increment in a worker thread, but updating the text view is done by main's thread handler.
Thread worker= new Thread(new Runnable() {
#Override
public void run() {
for (int i = 0; i < 10; i++) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// stop recording after 10 seconds
if (i == 9) {
handler.post(new Runnable() {
#Override
public void run() {
stopRecordingVideo();
}
});
}
else{
// set text for each second
handler.post(new Runnable() {
#Override
public void run() {
textView.setText(String.valueOf(i+1));
}
});
}
}//ends for()
worker.start()

android Handler.postDelayed use in many places

I have an app, which shows a video. At some point I want to give information about videos places. For example, historical places names. I am using this code:
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Do something after 100ms
}
}, 100);
But when I use this postDelayed comment in many times some message not show or overtake. What is the best solution to show some texts in a delay? For example 5 seconds later show A text, 15 sec later show B text, 30 sec later show C text. my codes look like this:
switch ()
case 1:
openVideo(video1)
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//my message
}
}, 4000);
break;
case 2:
openVideo(video2)
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
//my second message
}
}, 3000);
handler2.postDelayed(new Runnable() {
#Override
public void run() {
//my third message
}
}, 15000);
break;
Use multi-runnable.. Increase your duration for each text.
int mDuration=0,mAnimationDuration=5000;
mDuration=mDuration+mAnimationDuration;
//it called after 5 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your first text
}
}, mDuration);
mDuration=mDuration+mAnimationDuration;
//it called after 10 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your second text
}
}, mDuration);
mDuration=mDuration+mAnimationDuration;
//it called after 15 seconds
handler.postDelayed(new Runnable() {
#Override
public void run() {
//Your third text
}
}, mDuration);
Use Timer for every second...
new Timer().scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
//put you code here
//or set switch case for time 5,10,15 seconds
}
}, 0, 1000);//put here time 1000 milliseconds=1 second

How to use handler as a timer in android?

Handler handler = new Handler();
if (v.getId() == R.id.play){
handler.postDelayed(new Runnable() {
public void run() {
play.setBackgroundResource(R.drawable.ilk);
}
}, 2000);
play.setText("Play");
}
I want to set background first and then after 2 seconds later, code will continue next line which is play.setText("Play"); and goes like that. Instead of this, first text appears. 2 seconds later background changes.
Handler.postDelayed returns immediately. And next line is executed.
After indicated milliseconds, the Runnable will be executed.
So your code should be like this:
void doFirstWork() {
Handler handler = new Handler();
if (v.getId() == R.id.play){
handler.postDelayed(new Runnable() {
public void run() {
play.setText("Play");
doNextWork();
}
}, 2000);
play.setBackgroundResource(R.drawable.ilk);
}
}
void doNextWork() {
...
}
Set the background first. After that set the text within Handler. As you've put delays at the end of postDelayed so it'll fire right after that stated delays or in your case after 2 sec.
if (v.getId() == R.id.play){
play.setBackgroundResource(R.drawable.ilk);
new Handler().postDelayed(new Runnable() {
public void run() {
play.setText("Play");
}
}, 2000);
}

How make a button invisible for 1 or 2 second on another button click

In my app I want to make a button invisible for a few seconds after another button has been pressed and then it should become visible again.
How it is possible?
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
btn.setVisibility(View.VISIBLE);
}
}, 1000); // where 1000 is equal to 1 sec (1 * 1000)
}
});
You can do somthing like this:
firstBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
secondBtn.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
secondBtn.setVisibility(View.VISIBLE);
}
}, 2000); //change it for the time you need in milliseconds
}
});
must make buttonView invisible then use btnView.postDelayed
Just inside onClick of second button just do
secondButtonView.setVisibility(View.INVISIBLE);
secondButtonView.postDelayed(new Runnable() {
#Override
public void run() {
secondButtonView.setVisibility(View.VISIBLE);
}
}, 2000);
View.postDelayed() simply calls Handler.postDelayed(). It's a
convenient method that helps avoid creating Handler instances.
This quote is from Romain Guy Android framework engineer https://groups.google.com/forum/#!topic/android-developers/IuG3HgKx89Q
//my button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Do something after 5s = 5000ms
//my button visible
}
}, 5000);
U can use handler for it
also u can use Timer and TimerTask
//First button invisible
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// Second visible
// after some MS
}
}, 2000);
Suppose you have two buttons Button button1,button2 properly inflated and displayed in view. You can change visibility of button2 on click of button1 by:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button2.setVisibility(View.INVISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button2.setVisibility(View.VISIBLE);
}
}, 2 * 1000);//number of seconds *1000
}
});
try this,
write following code on another button's click event.
continuebutton.setVisibility(View.INVISIBLE);
continuebutton.postDelayed(new Runnable() {
public void run() {
continuebutton.setVisibility(View.VISIBLE);
}
}, 2000);

Android: use handler post.delayed twice

I would like to know if it's possible to use handler().postdelayed twice?
I mean, I want to create a button, that when clicked it change the color and stay in this state 1 second, then, after 1 second another button change the color.
I've created the following code:
In the onclicklistener:
btn3.setBackgroundColor(Color.WHITE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
checkAnswer();
waitAnswer();
btnRsp3.setBackgroundResource(R.drawable.selector);
}
}, 1000);
CheckAnswer:
public void CheckAnswer(){
btn1.setBackgroundColor(Color.GREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
}
}, 500);
btn1.setBackgroundResource(R.drawable.selector);
}
I think the problem is on CheckAnswer because it seems it doesn't stop in this postDelayed and step to the waitAnswer.
Thanks
Why do you expect it to stop on postDelayed? postDelayed places your Runnable to the Handler Looper queue and returns. Since both handlers are created on the same looper, the second runnable is executed after the first one terminates (plus whatever left of the 500 ms delay)
UPDATE:
You need something like that
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundColor(Color.GREEN);
}
}, 1000);
handler.postDelayed(new Runnable() {
#Override
public void run() {
btn1.setBackgroundResource(R.drawable.selector);
}
}, 2000);
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
//Your Work
}
}, 1000);

Categories

Resources