Reputation: 119
Currently I have a counter that is subtracted every second, how can I add more time to the freetime variable when I click on the add time button?
if I add the variables before clicking the start button it works, but once the counter is active it does not add more time to the counter
TextView count_txt;
Button start_button,moretime_button;
int freetime = 1800000;
String time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.count_layout);
CountActivity.this.setTitle("CountDown");
//find view
start_button = findViewById(R.id.startcount_button);
count_txt = findViewById(R.id.textView_count);
moretime_button = findViewById(R.id.button_moretime);
moretime_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//add button
}
});
start_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new CountDownTimer(netTime,1000) {
@Override
public void onTick(long millisUntilFinished) {
time = String.format(Locale.getDefault(),"Time Remaining" + "\n" + "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished)%60,
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)% 60,
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)% 60);
count_txt.setText((time));
netTime--;
}
@Override
public void onFinish() {
count_txt.setText("FINISH!!!");
}
}.start();
}
});
Upvotes: 1
Views: 187
Reputation: 1545
Here is the source code you can start, stop, pause, add & subtract the timer in your code. This is a simple library you can add in your project or add a simple module to your project. Here is an example code
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var countDownTimerWithPause: SonicCountDownTimer
private lateinit var progressIndicator: CircularProgressIndicator
private lateinit var tvTimerCountFinished: TextView
private lateinit var tvTimerCountTotal: TextView
private lateinit var tvTitleSec:TextView
private lateinit var btnAddSec: Button
private lateinit var btnReset: Button
private lateinit var btnPlayPause: FloatingActionButton
private var timeSec: Int = 10
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initViews()
clickMethod()
}
@SuppressLint("SetTextI18n")
private fun initViews() {
progressIndicator = findViewById(R.id.progress_indicator)
tvTimerCountFinished = findViewById(R.id.tv_timer_count_finished)
tvTimerCountTotal = findViewById(R.id.tv_timer_count_total)
tvTitleSec = findViewById(R.id.tv_title_sec)
btnAddSec = findViewById(R.id.btn_add_sec)
btnReset = findViewById(R.id.btn_reset)
btnPlayPause = findViewById(R.id.btn_playpause_timer)
initCountdownTimer()
tvTimerCountFinished.text = "\"00"
tvTimerCountTotal.text = "|\"$timeSec"
tvTitleSec.text = "$timeSec"
}
@SuppressLint("SetTextI18n")
private fun clickMethod() {
btnPlayPause.setOnClickListener {
if (countDownTimerWithPause.isTimerRunning()){
if (countDownTimerWithPause.isTimerPaused()) {
countDownTimerWithPause.resumeCountDownTimer()
btnPlayPause.setImageResource(R.drawable.ic_baseline_pause_24)
} else {
countDownTimerWithPause.pauseCountDownTimer()
btnPlayPause.setImageResource(R.drawable.ic_baseline_play_24)
}
}else{
countDownTimerWithPause.startCountDownTimer()
btnPlayPause.setImageResource(R.drawable.ic_baseline_pause_24)
}
}
btnAddSec.setOnClickListener {
if (!countDownTimerWithPause.isTimerRunning()){
timeSec+= 10
progressIndicator.max = timeSec
countDownTimerWithPause.setCountDownTime((timeSec*1000).toLong())
tvTimerCountTotal.text = "|\"$timeSec"
tvTitleSec.text = "$timeSec"
}
}
btnReset.setOnClickListener {
countDownTimerWithPause.stopCountDownTimer()
timeSec = 10
progressIndicator.progress = 0
tvTimerCountFinished.text = "\"00"
tvTimerCountTotal.text = "|\"$timeSec"
tvTitleSec.text = "$timeSec"
}
}
@SuppressLint("SetTextI18n")
private fun initCountdownTimer() {
var mCount: Int
progressIndicator.max = timeSec
countDownTimerWithPause =
object : SonicCountDownTimer((timeSec * 1000).toLong(), 1000) {
override fun onTimerTick(timeRemaining: Long) {
mCount = timeSec - (timeRemaining / 1000).toInt()
progressIndicator.progress = mCount
tvTimerCountFinished.text = "\"$mCount"
tvTimerCountTotal.text = "|\"$timeSec"
}
override fun onTimerFinish() {
btnPlayPause.setImageResource(R.drawable.ic_baseline_play_24)
}
}
}
override fun onDestroy() {
super.onDestroy()
countDownTimerWithPause.cancelCountDownTimer()
}
}
SonicCountDownTimer.kt
abstract class SonicCountDownTimer : SonicCountDownTimerListener {
/**
* To maintain Timer start and stop status.
*/
private var isTimerRunning = false
/**
* To maintain Timer resume and pause status.
*/
private var isTimerPaused = false
/**
* Timer time.
*/
private var mTime: Long = 0
private var localTime: Long = 0
private var timeInterval: Long = 0
private var mHandler: Handler? = null
constructor() {
init(0, INTERVAL.toLong())
}
constructor(timeInMillis: Long) {
init(timeInMillis, INTERVAL.toLong())
}
constructor(timeInMillis: Long, intervalInMillis: Long) {
init(timeInMillis, intervalInMillis)
}
/**
* Method to initialize [SonicCountDownTimer].
*
* @param time: Time in milliseconds.
* @param interval: in milliseconds.
*/
private fun init(time: Long, interval: Long) {
setCountDownTime(time)
setTimeInterval(interval)
initSonicCountDownTimer()
}
private fun initSonicCountDownTimer() {
mHandler = object : Handler(Looper.getMainLooper()) {
override fun handleMessage(msg: Message) {
super.handleMessage(msg)
if (msg.what == MSG) {
if (!isTimerPaused) {
if (localTime <= mTime) {
onTimerTick(mTime - localTime)
localTime += timeInterval
sendMessageDelayed(mHandler!!.obtainMessage(MSG), timeInterval)
} else stopCountDownTimer()
}
}
}
}
}
/**
* Convenience method to check whether the timer is running or not
*
* @return: true if timer is running, else false.
*/
fun isTimerRunning(): Boolean {
return isTimerRunning
}
/**
* Method to start the CountDownTimer.
*/
fun startCountDownTimer() {
if (isTimerRunning) return
isTimerRunning = true
isTimerPaused = false
localTime = 0
mHandler!!.sendMessage(mHandler!!.obtainMessage(MSG))
}
/**
* Method to stop the CountDownTimer.
*/
fun stopCountDownTimer() {
isTimerRunning = false
mHandler!!.removeMessages(MSG)
onTimerFinish()
}
/**
* Method to cancel the CountDownTimer.
*/
fun cancelCountDownTimer() {
isTimerRunning = false
mHandler!!.removeMessages(MSG)
}
/**
* Method to check whether the CountDownTimer is paused.
*
* @return: true if CountDownTimer is paused else false.
*/
@Synchronized
fun isTimerPaused(): Boolean {
return isTimerPaused
}
/**
* To pause the timer from Main thread.
*
* @param isPaused: true to pause the timer, false to resume.
*/
@Synchronized
private fun setTimerPaused(isPaused: Boolean) {
this.isTimerPaused = isPaused
}
/**
* Convenience method to pause the timer.
*/
@Synchronized
fun pauseCountDownTimer() {
setTimerPaused(true)
}
/**
* Convenience method to resume the timer.
*/
@Synchronized
fun resumeCountDownTimer() {
setTimerPaused(false)
mHandler!!.sendMessage(mHandler!!.obtainMessage(MSG))
}
/**
* Setter for Time.
*
* @param timeInMillis: in milliseconds
*/
fun setCountDownTime(timeInMillis: Long) {
var timeInMillis = timeInMillis
if (isTimerRunning) return
if (mTime <= 0) if (timeInMillis < 0) timeInMillis *= -1
mTime = timeInMillis
}
/**
* @return remaining time
*/
fun getRemainingTime(): Long {
return if (isTimerRunning) {
mTime
} else 0
}
/**
* Setter for time interval.
*
* @param intervalInMillis: in milliseconds
*/
fun setTimeInterval(intervalInMillis: Long) {
var intervalInMillis = intervalInMillis
if (isTimerRunning) return
if (timeInterval <= 0) if (intervalInMillis < 0) intervalInMillis *= -1
timeInterval = intervalInMillis
}
companion object {
private const val INTERVAL = 1000
private const val MSG = 1
}
}
SonicCountDownTimerListener.kt
interface SonicCountDownTimerListener {
/**
* Method to be called every second by the [SonicCountDownTimer]
*
* @param timeRemaining: Time remaining in milliseconds.
*/
fun onTimerTick(timeRemaining: Long)
/**
* Method to be called by [SonicCountDownTimer] when the thread is getting finished
*/
fun onTimerFinish()
}
Screen
Upvotes: 1