Advertisement
Guest User

Untitled

a guest
May 24th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. package com.example.falk.eggtimer;
  2.  
  3. import android.media.MediaPlayer;
  4. import android.os.CountDownTimer;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.SeekBar;
  10. import android.widget.TextView;
  11.  
  12.  
  13. public class eggTimer extends AppCompatActivity {
  14.  
  15.     int maxTime = 3000*600;
  16.     boolean timerActive = false;
  17.  
  18.     TextView counter;
  19.     String timeLeftFormatted;
  20.     MediaPlayer alarm;
  21.     SeekBar seekBar;
  22.     Button button;
  23.     CountDownTimer countdownTimer;
  24.  
  25. public void onClick (View view){
  26.  
  27.     if (!timerActive){
  28.         countdownTimer = new CountDownTimer(seekBar.getProgress(), 1000) {
  29.             @Override
  30.             public void onTick(long millisUntilFinished) {
  31.                 updateCountdownText(counter, millisUntilFinished);
  32.             }
  33.  
  34.  
  35.             @Override
  36.             public void onFinish() {
  37.                 alarm.start();
  38.  
  39.             }
  40.         }.start();
  41.     button.setText("Stop");
  42.     timerActive = true;
  43.     }
  44.     else { countdownTimer.cancel();
  45.         timerActive = false;
  46.         button.setText("Start");
  47.     }
  48. }
  49.  
  50.     public void updateCountdownText(TextView counter, long remaining){
  51.  
  52.         int minutes = (int) remaining / 1000 / 60;
  53.         int seconds = (int) remaining / 1000 % 60;
  54.  
  55.         timeLeftFormatted = String.format("%02d:%02d", minutes, seconds);
  56.         counter.setText(timeLeftFormatted);
  57.  
  58.  
  59.     }
  60.  
  61.  
  62.     @Override
  63.     protected void onCreate(Bundle savedInstanceState) {
  64.         super.onCreate(savedInstanceState);
  65.         setContentView(R.layout.activity_egg_timer);
  66.  
  67.         counter = findViewById(R.id.editText);
  68.         button = findViewById(R.id.button);
  69.         seekBar = findViewById(R.id.seekBar);
  70.         seekBar.setMax(maxTime);
  71.  
  72.         alarm = MediaPlayer.create(this, R.raw.franz);
  73.  
  74.  
  75.         seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
  76.             @Override
  77.             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
  78.  
  79.                 if (!timerActive) {
  80.                     if (fromUser) {
  81.  
  82.                         updateCountdownText(counter, progress);
  83.  
  84.                     }
  85.                 }
  86.  
  87.  
  88.             }
  89.  
  90.             @Override
  91.             public void onStartTrackingTouch(SeekBar seekBar) {
  92.  
  93.  
  94.             }
  95.  
  96.             @Override
  97.             public void onStopTrackingTouch(SeekBar seekBar) {
  98.  
  99.  
  100.             }
  101.         });
  102.  
  103.  
  104.  
  105.  
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement