Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.falk.eggtimer;
- import android.media.MediaPlayer;
- import android.os.CountDownTimer;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.SeekBar;
- import android.widget.TextView;
- public class eggTimer extends AppCompatActivity {
- int maxTime = 3000*600;
- boolean timerActive = false;
- TextView counter;
- String timeLeftFormatted;
- MediaPlayer alarm;
- SeekBar seekBar;
- Button button;
- CountDownTimer countdownTimer;
- public void onClick (View view){
- if (!timerActive){
- countdownTimer = new CountDownTimer(seekBar.getProgress(), 1000) {
- @Override
- public void onTick(long millisUntilFinished) {
- updateCountdownText(counter, millisUntilFinished);
- }
- @Override
- public void onFinish() {
- alarm.start();
- }
- }.start();
- button.setText("Stop");
- timerActive = true;
- }
- else { countdownTimer.cancel();
- timerActive = false;
- button.setText("Start");
- }
- }
- public void updateCountdownText(TextView counter, long remaining){
- int minutes = (int) remaining / 1000 / 60;
- int seconds = (int) remaining / 1000 % 60;
- timeLeftFormatted = String.format("%02d:%02d", minutes, seconds);
- counter.setText(timeLeftFormatted);
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_egg_timer);
- counter = findViewById(R.id.editText);
- button = findViewById(R.id.button);
- seekBar = findViewById(R.id.seekBar);
- seekBar.setMax(maxTime);
- alarm = MediaPlayer.create(this, R.raw.franz);
- seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
- @Override
- public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
- if (!timerActive) {
- if (fromUser) {
- updateCountdownText(counter, progress);
- }
- }
- }
- @Override
- public void onStartTrackingTouch(SeekBar seekBar) {
- }
- @Override
- public void onStopTrackingTouch(SeekBar seekBar) {
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement