BetterTimer is the fastest way to implement a timer in Android. It solves some of CountDownTimer's problems 1 and provides a robust API for interfacing 2 with the timer
Add this to your app's project-level build.gradle
file:
repositories {
maven { url 'https://jitpack.io' }
}
And this to your app's module-level build.gradle
file:
dependencies {
implementation 'com.github.gitryder:better-timer:v0.2-alpha'
}
- Have your
Activity
implementBetterTimer.OnTimerTickListener
- class MainActivity : AppCompatActivity() { ... }
+ class MainActivity : AppCompatActivity(), BetterTimer.OnTimerTickListener { ... }
- Create an instance of
BetterTimer
- Pass the time (in minutes)
- Pass the activity context (this) to receive callbacks
val timer = BetterTimer(25, this)
- Call the timer methods as you please
timerStartButton.setOnClickListener {
timer.start()
// Update UI Logic
}
timerPauseButton.setOnClickListener {
timer.pause()
// Update UI Logic
}
timerResetButton.setOnClickListener {
timer.reset()
// Update UI Logic
}
- Update the view that displays the time, in the
onTimerTick()
callback
override fun onTimerTick(timeUntilFinished: String) {
timerDisplayTextview.text = timeUntilFinished
}
- Fork this repo (git clone https://github.com/gitryder/better-timer.git)
- Create a feature branch (git checkout -b a-new-feature)
- Commit your work (git commit -am 'Add new feature')
- Push to your branch (git push origin a-new-feature)
- Create a shiny new Pull Request
- Pat yourself on the back!
Built with ❤︎ by Danyl Fernandes
- Website (WIP)
- Twitter (https://twitter.com/androidanyl)
- LinkedIn (https://www.linkedin.com/in/danyl-fernandes-5bb706157/)
- Not a "problem" per se, but every tick of the
CountDownTimer
is calculated using the duration of the previous tick, which is what makes the ticks inaccurate as the timer run-time goes up. - BetterTimer provides convenience methods (
timer.start()
,timer.pause()
,timer.reset()
) to make setting up a timer in your app AS EASY AS CAN BE.
Copyright 2020 Danyl Fernandes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.