0

I would like to know if it's possible to increment seconds, hours, or minutes while the timer is running. Currently I have implemented this answer in my apps feedAdapter. I also used this kind of incremental function in CountDownTimer and it really worked out!, but only when you pause it and then resume it again.(Let's not get to it). All the handlers and functions are in this links's answer!

I have 2 button in the adapter: 1: Start's the timer 2: I want this button to increment seconds or minutes etc. Would appreciate any help that gets through!

How to handle multiple countdown timers in ListView?

public void incrementExpirationTime () {

       int defaultIncrementValue = 10000; //lets say ten seconds (this can also be a long data)
       long productExpiryTime = getProductExpiryTime();
       productExpiryTime+=defaultIncrementValue;
}

I also made this private long productExpiryTime; an instance but that also didn't help.

Community
  • 1
  • 1
mikaeel
  • 57
  • 6
  • Ok have 2 buttons and a textview? So for instance want the textview at 12 seconds to change to 22 on the click and continue counting 23,24,25,26. If so that should be very easy – Xjasz Jan 07 '16 at 18:57
  • @Jasz no! it must start continuing from 26,25,24,23 etc. Please refer to the link! – mikaeel Jan 07 '16 at 18:59
  • @Jasz and sir i am using `button.setText(String);` – mikaeel Jan 07 '16 at 19:03

1 Answers1

0

You can tinker with it to get it better. This is how I did it take what you think is useful let me know if you have a question.

private final int INCREMENT = 10;
private final int ONE_SECOND = 1;
private boolean running = false;
private int time = 100;

public void initialize() {
    final Button start = findViewById(R.id.start);
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            running = !running;
            if (running)
                updateButton(start);
        }
    });

    final Button increment = findViewById(R.id.increment);
    increment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (running)
                time += INCREMENT;
        }
    });
}

private void updateButton(final Button start) {
    start.postDelayed(new Runnable() {
        @Override
        public void run() {
            time--;
            start.setText(String.valueOf(time));
            if (time == 0) {
                Toast.makeText(getApplicationContext(), "time is up!", Toast.LENGTH_SHORT).show();
                running = false;
            } else {
                if (running) {
                    updateButton(start);
                }
            }
        }
    }, ONE_SECOND);
}
Xjasz
  • 1,238
  • 1
  • 9
  • 21
  • i started it at 100 and everytime you do time-- it shows what it was so 100 then decrements it.. Its equivalent to time = time - 1; one sec i need to make and edit – Xjasz Jan 07 '16 at 20:01