How to create a java code for android that waits for some time , like 3 - 5 secs for the user to perform a button click which just changes its xy coordinates and increments a score , else intents to another activity.
I have tried to implement it by referring to this SO answer [ How to pause / sleep thread or process in Android? ,but the code keeps intenting to another activity after 5-6 button clicks even if i click within the specified delay time. This is the code -
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
score++;
scr=String.valueOf(score);
scoredisp.setText(scr);
int xval=xvalue();
int yval=yvalue();
btn.setX(xval);
btn.setY(yval);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run()
{
Intent result = new Intent(MainActivity.this,com.example.warlock.buttonsquash.result.class);
startActivity(result);
}
}, 3000);
}
});
int xvalue() {
Random randomGenerator = new Random();
int xval = randomGenerator.nextInt(850) + 1;
return xval;
}
int yvalue() {
Random randomGenerator = new Random();
int yval = randomGenerator.nextInt(1500) + 1;
return yval;
}
I just need button click to change the button coordinates and increment a score value but the intent starts happening altogether after 5-6 button-clicks.