0

I am creating a simple countdown timer in android studio which has a start and a pause button. The timer countdown starts but does not pause instead it starts increasing and decreasing by clicking pause button. Here is my code.

<TextView
        android:id="@+id/txtview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Counter"
        android:textSize="75sp"
        android:textColor="#ff0000"
        />
    <Button
        android:id="@+id/push_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Count Down"
        android:onClick="perform_action"
        android:textSize="35sp"
        />
    <Button
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause Count Down"
        android:onClick="perform_action"
        android:textSize="35sp"
        />

Java code

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void perform_action(View view)
    {
    long totalMilliseconds = 90000;
    long interval = 1;

        Button b1 = (Button)findViewById(R.id.push_button);
        String push = b1.getText().toString();

        CountDownTimer timer;
        timer = new CountDownTimer(40000, 1000) {

            TextView t1 = (TextView)findViewById(R.id.txtview1);
            @Override
            public void onTick(long l) {
                t1.setText(""+(l));
            }

            @Override
            public void onFinish() {
                t1.setText("Finish");
            }
        };
        if(push.equals("Start Count Down")){


            timer.start();
        }
        else if(push.equals("Pause Count Down")){
            timer.cancel();
        }

    }
}

Please provide me some help

Satnam123
  • 35
  • 7
  • You are re-defining the counter everytime you click a button. You'd wanna put the timer in a scope that it lives along the Activity's lifecycle. Then inside the `perform_action` method you should update that global timer. See [this answer](https://stackoverflow.com/a/4598737/9858236) and please do some research in the related topic before directly asking your specific problem. – alegria Jul 08 '18 at 11:02

1 Answers1

0

It looks like you're spawning a new timer every time you click the button, from the snippet you posted. You can just use an OnClickListener.

You can access it like so (example uses a boolean to switch states);

Boolean ButtonClicked = false;

  b1.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //Perform your click logic to start and stop the timer
 if (!ButtonClicked)) {
            ButtonClicked = true;
            timer.start(); 
        } else {
            ButtonClicked = false;
            timer.cancel();
        }                       
            }
        });