How to make timer stop on click - java

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():
if (t != null)
t.cancel();
and remove the stopping condition from the timer.
Code Example (updated):
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
// Removed the stopping condition/counter
}
});
}
}, 1000, 1000); // Do you really want to wait 1 second before executing the timer's code? If not, change the 1st "1000" to a "0"
final Button b = (Button) findViewById(R.id.my_button_id); // Replace with your button's id
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (t != null)
t.cancel();
}
});

Use a CountDownTimer. When the button is clicked just stop the timer.
But for what you have just go ahead and create a button, set an OnClickListener on the button and then call timer.cancel() or whatever stops it inside on the onClick() method of your listener.

Related

How to start a thread if a button is idle?

I need a thread to start after 3 seconds of a button being idle, is there a simple way of doing it?
I'm building a counter app, the button triggers two counters, the total counter and the "tapping counter", the tapping counter helps keep track of the actual change of values, showing how many taps the user did, I need it to vanish after some seconds so the user can tap again.
for stuffs like that I usually use a Handler with a Runnable in order to do stuff after X milliseconds the user isn't doing a specific action.
First, create a runnable and a handler
final android.os.Handler handler = new android.os.Handler();
private Runnable runnable;
private final long DELAY = 3000; // how many milliseconds you want to wait
Then add the onClickListener:
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
Then, inside onClick event, remove callbacks and istantiate the handler again as follows:
if(runnable != null) {
// in this case the user already clicked once at least
handler.removeCallbacks(runnable);
}
runnable = new Runnable() {
#Override
public void run() {
//this code will run when user isn't clicking for the time you set before.
}
};
handler.postDelayed(runnable, DELAY);
Final result:
final android.os.Handler handler = new android.os.Handler();
private Runnable runnable;
private final long DELAY = 3000; // how many milliseconds you want to wait
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// all your previous stuffs
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(runnable != null) {
// in this case the user already clicked once at least
handler.removeCallbacks(runnable);
}
runnable = new Runnable() {
#Override
public void run() {
//this code will run when user isn't clicking for the time you set before.
}
};
handler.postDelayed(runnable, DELAY);
}
});
}
I hope this helps, for any question feel free to ask
Handler may work in this scenario, with a 3000 milisecond delay.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// do action
}
}, 3000);
At first, you create a Timer with a TimerTask(with your Thread) and schedule it to run after 3 seconds.
Every time the button is pressed, you reset the timer.
public class MyClass{
private Timer timer=new Timer()
private TimerTask task=new TimerTask(){
public void run(){
//your action
}
};
public void init(){
timer.schedule(task,3000);
}
public void onButtonClick(){
task.cancel();
timer.schedule(task,3000);
}
}

how to set different onclicklisteners on same button click?

I am implementing a timer in android with one text view and one button for start/stop.
How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events?
I am implementing a timer in android with one text view and one button for start/stop.
How do I set register different events on clicklistener of the same button, such that when it is clicked the first time it will start a timer and when clicked a second time it will stop the timer and report the time between events?
Edit1
what i did is,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_depth);
findViewById(R.id.btn).setOnClickListener(this);
}
boolean showingFirst = true;
public void generate(View view){
if(showingFirst){
long startTime = System.currentTimeMillis();
showingFirst = false;
}else{
long difference = System.currentTimeMillis() - startTime;
showingFirst = true;
TextView myText = (TextView)findViewById(R.id.tv);
myText.setText(String.valueOf(difference));
}
}
but since long starttime is started in if when the control enters else loop it shows
cannot resolve symbol 'startTime'
please help and special thanks to eliamyro
You can do it using a global boolean isStart and start or stop the timer depending on the value of the isStart.
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isStart) {
// Stop timer
isStart = false;
} else {
// Start timer
isStart = true;
}
}
});
try this,
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().equals("Start")){
btn.setText("Stop");
// start timer
}else{
btn.setText("Start");
// stop timer
}
}
});
on click (start/stop) button start the timer according to your code and return a value to button and when you click again that flag value can be used to create a if condition for stop as well as start
As you are starting and stopping a Timer with your button you can just check if the timer is running or not. I would suggest extending a TimerTask for that use case (I took that code from here):
public class YourTimerTask extends TimerTask {
private boolean isRunning = false;
#Overrides
public void run() {
this.isRunning = true;
//rest of run logic here...
}
public boolean isRunning() {
return this.isRunning;
}
}
Then in your onClickListener you can just check if your timer is running or not:
startStopBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (yourTimer.isRunning()) {
stopTimer();
} else {
startTimer();
}
}
});

Display a Toast when a button is clicked frequently

I am new to Android development so excuse me for this question.
So I have a button that when clicked, it will call a method called btnDelay(btnName).
Inside that method is this line of codes:
private void btnDelay(final Button btn){
btn.setEnabled(false);
/*if (counter == 0){
counter++;
}*/
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
btn.setEnabled(true);
}
});
}
}, 5000);
}
That will disable the button for 5 seconds.
Now what I want to do is when the user clicks the button again and the 5 seconds is not finished, will display a Toast stating that the user's action is too frequent.
Is there a way I can do this? I am thinking of using a counter that will count how many times the user clicked that specific button and will reset to 0 after the 5 seconds on the TimerTask is done. But is there a simplier way to do that? Thank you.
Your button won't fire an onClick event if it's disabled. So instead of disabling it, set the colours to grey or something so it looks disabled and then in your onClick handler for the button:
if(enabled){
btnDelay();
}
else {
sendAToast();
}
Then in btnDelay(), set enabled = false (and set the colours to grey if you want), and inside run() set enabled = true.
Also don't forget to private boolean enabled = true at the top of your class :)
You should declare a Boolean variable for button state. Because if you write btn.setEnabled(false); ,buttonClickEvent can not be triggered for five seconds.
boolean btnState = true;
private void btnDelay(final Button btn){
if (btnState){
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
btnState = false;
}
});
}
}, 5000);
}else{
Toast.makeText(this, "your_message", Toast.LENGTH_SHORT).show();
}
}

Making the count down timer skip 5 seconds when button is pressed (Android studios)

I'm working on an Android application where I would like the CountdownTimer to skip 5 seconds when the reset button is pressed. For example, if the reset button is pressed when count down timer is at t = 15, the timer will skip the next five seconds and hence the timer will now display t = 10. Here's what I tried but it didn't work:
Button Code:
buttonReset = (Button) findViewById(R.id.ButtonReset);
buttonReset.setOnClickListener(
new OnClickListener() {
#Override
public void onClick(View v) {
TextView output = (TextView) findViewById(R.id.Display);
r = 1;
output.setText("");
resetStatus = true;
}
}
);
Edit: I have updated the timer code. Now, I cancel the old timer and start a new one.
Count down timer code:
public void start()
{
new CountDownTimer(59*1000, 1000)
{
TextView timer = (TextView) findViewById(R.id.Timer);
public void onTick(long millisUntilFinished)
{
int TimeSeconds = (int) millisUntilFinished/1000;
if (resetStatus)
{
cancel();
TimeSeconds = TimeSeconds - 5;
final int finalTimeSeconds = TimeSeconds;
new CountDownTimer(finalTimeSeconds*1000, 1000)
{
#Override
public void onTick(long millisUntilFinished) {
if (finalTimeSeconds >= 10)
{
//displays time
timer.setText("00:" + finalTimeSeconds);
}
else
{
//displays time
timer.setText("00:0" + finalTimeSeconds);
}
resetStatus = false;
}
#Override
public void onFinish() {
gameOverDialogue();
}
}.start();
}
else
{
if (TimeSeconds >= 10)
{
//displays time
timer.setText("00:" + TimeSeconds);
}
else
{
//displays time
timer.setText("00:0" + TimeSeconds);
}
}
}
public void onFinish()
{
gameOverDialogue();
}
}.start();
}
When I press the reset button now, the timer skips 5 seconds after which the display freezes. However, the timer keeps ticking and reaches 0. I think it's because in the second count down timer the final variable finalTimeSecondsis used instead of TimeSeconds. Any suggestions?
Thanks in advance
In your code you only modify, one time, the time to display, but your CountDownTimer is not affected by this edit.
You should restart another CountDownTimer with the new delay and cancel the current one.
The problem is that your display changes with your flag, but the timer doesn't. Your assignment
int TimeSeconds = (int) millisUntilFinished/1000;
runs on every tick. You check the resetStatus flag and adjust the TextView, but that has no effect on the actual timer. The next time it ticks, it will have the proper (undeducted) time remaining.
Solving this problem can be kind of tricky. You can, as #MoraS suggested, create a new CountdownTimer each time a deduction is warranted. This solution will work, but if you expect deductions often, it may slow down the app a bit.
My suggestion would be to keep track of the total number of deductions, use that to keep the TextView up to date, and cancel the timer early as appropriate.

Android stop loop by click button

i am trying to make a button that when its clicked , it changes its color image and starts a countdowntimer in a method activeDelay() as here:
piscaAutoButton = (Button) rootView.findViewById(R.id.piscaAutoButton);
piscaAutoButton.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(final View view) {
if (sessionManager.getPisca()) {
sessionManager.setPisca(false);
trigger = false;
piscaAutoButton.setBackgroundResource(R.drawable.button_bg_round);
} else {
sessionManager.setPisca(true);
piscaAutoButton.setBackgroundResource(R.drawable.button_add_round);
trigger = true;
activeDelay(trigger);
}
here is my activeDelay method:
private boolean activeDelay(boolean trigger) {
while (trigger) { // LOOP WHILE BUTTON IS TRUE CLICKED
int timerDelay = manualControl.getDelayPisca(); //input for timer
//delay manual
new CountDownTimer(timerDelay * 1000, 1000) {
public void onFinish() {
System.out.println("sent");
try {
System.out.println("blink button " + manualControl.getBlinkButton());
if (!manualControl.getBlinkButton().isEmpty()) {
MenuActivity.mOut.write(manualControl.getBlinkButton().getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void onTick(long millisUntilFinished) {
}
}.start();
}
return trigger;
}
My problem is that i need the counter keeps going after finished, stopping just when the user clicks again in the button (trigger = false). I am having problems to program that, if someone could help,i know the return inside activeDelay ejects from the method, how can we solve that ,tks
I would suggest you to don't use CountDownTimer(this runs for some specific time period) , instead of this you should use Handler(this run infinitely) . i am sending you handler code.
private Handler handler = new Handler();
//call this when you want to start the timer .
handler.postDelayed(runnable, startTime);
Runnable runnable = new Runnable() {
#Override
public void run() {
// Do here , whatever you want to do(show updated time e.t.c.) .
handler.postDelayed(this, xyz); //xyz is time interval(in your case it is 1000)
}
};
//Stop handler when you want(In your case , when user click the button)
handler.removeCallbacks(runnable);

Categories