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);
Related
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
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);
}
From my generic fragment, I have this method:
protected void loadDataListWithDelay() {
Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
loadDataList();
}
}, DELAY_START_LOADING);
}
This method allows to start a new loading data from server (with Volley networking).
In my loadDataList() method there are some views visibility setting:
protected void loadDataList(String url, ArrayList<BaseFilters> filters,
String query, boolean byPassSearchMode) {
...
mLoadingDataListView.setVisibility(View.INVISIBLE);
mListContainer.setVisibility(View.VISIBLE);
...
This code runs perfectly, but I have this crash this morning on mobile with Android 6.0.1.
Could you help me guys?
I will give you a good advice, if you want to do postDelayed, take a view in your fragment (or any view), and do postDelayed with it. i.e. textView.postDelayed(.. This way you can be sure you are on the ui thread.
Why not use runOnUiThread instead of creating a new handler ?
runOnUiThread(new Runnable() {
#Override
public void run() {
loadDataList();
}
});
If you need a delay, then you can use a handler inside the runOnUiThread
runOnUiThread(new Runnable() {
#Override
public void run() {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadDataList();
}
}, DELAY);
}
});
I am using postDelayed with TextView to hide it after some time. Now, I want to remove postDelayed if user click on button.
My code is as below :
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
tvRQPoint.setText("");
}
}, 10000);
How to do this ?
Create your thread in separate place below...
private Runnable mTimerExecutor = new Runnable() {
#Override
public void run() {
tvRQPoint.setText("");
}
};
Then call it as follows to execute....
tvRQPoint.postDelayed(mTimerExecutor, 10000);
When you want to cancel the postDelay execution then cancel as follows...
tvRQPoint.removeCallbacks(mTimerExecutor);
check this
Runnable runnable = new Runnable() {
public void run() {
tvRQPoint.setText("");
}
};
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(runnable, 10000);
to remove it
tvRQPoint.removeCallbacks(runnable);
boolean clicked=false;
onClick event
clicked=true;
and in postDelayed
tvRQPoint.setText("+100");
tvRQPoint.postDelayed(new Runnable() {
public void run() {
if(!clicked)
tvRQPoint.setText("");
}
}, 10000);
Use below code inside onClick. It will remove.
private final Runnable r = new Runnable() {
public void run() {
tvRQPoint.setText("");
Handler handler = new Handler();
handler.postDelayed(this, 2000);
}
}
};
and then use this inside onClick of button
handler.removeCallbacks(r);
For more information check this link
I want to change the image in imageView after 5 seconds from app start.
This is the code that I tried so far:
public class MainActivity extends Activity {
ImageView screen;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screen=(ImageView)findViewById(R.id.imageView1);
screen.setImageResource(R.drawable.ic_launcher);
}
}
You can use a Handler, such as:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
As Sam says in the comments, you could also do this (because all Views have their own handler):
screen.postDelayed(new Runnable() {
#Override
public void run() {
// change image
}
}, 5000); // 5000ms delay
See the Handler Documentation.
you can try thread like this:
new Thread(){
public void run(){
//sleep(5000);
//refreshSthHere();
}
}.start();
You can use Handler like Tom Leese said or Java Timer
new Timer().schedule(new TimerTask() {
#Override
public void run() {
// this code will be executed after 2 seconds
}
}, 2000);