My timer will stop when it reaches a certain number. Instead, I want it to stop on a button click. How do I do that?
This is what my code looks like currently:
final TextView t1 = (TextView) findViewById(R.id.yourpay);
final Timer t =new Timer();
t.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
money = (PPS+Reserve);
Reserve = (money);
t1.setText("$" + money); //Place your text data here
counter++;
//Place your stopping condition over here. Its important to have a stopping condition or it will go in an infinite loop.
if(counter == HPDPS)
t.cancel();
}
});
}
}, 1000, 1000);
If possible I would like it to stop on button click AND when counter reaches HPDPS.
Put in your button’s
onClickListener():and remove the stopping condition from the timer.
Code Example (updated):