Reputation: 1115
I have been trying to implement a count down timer using handler in a simple text view. I need to set a custom time. for ex., the timer should start from 06:12:00.
-- This is the MainActivityenter code here
code --.
public class MainActivity extends AppCompatActivity {
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
long timeInMilliseconds = 0L;
long timeSwapBuff = 0L;
long updatedTime = 0L;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValue = (TextView) findViewById(R.id.timerValue);
startButton = (Button) findViewById(R.id.startButton);
--listener for start button --
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
pauseButton = (Button) findViewById(R.id.pauseButton);
--listener for pause button--
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
}
private Runnable updateTimerThread = new Runnable() {
public void run()
{
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
int secs = (int) (updatedTime / 1000);
int mins = secs / 60;
int hours = mins/60;
secs = secs % 60;
timerValue.setText(String.format("%02d", hours)+":" +
(String.format("%02d", mins)) + ":"
+ String.format("%02d", secs) );
customHandler.postDelayed(this, 0);
}
};}
The output i am getting is here https://ibb.co/pd95Yww
Upvotes: 1
Views: 250
Reputation: 106
public class MainActivity extends AppCompatActivity {
long timeInMilliseconds = 0L;
long timeSwapBuff = 22320000; // 6Hours:12Minutes:00Seconds
long updatedTime = 0L;
private Button startButton;
private Button pauseButton;
private TextView timerValue;
private long startTime = 0L;
private Handler customHandler = new Handler();
private Runnable updateTimerThread = new Runnable() {
public void run() {
timeInMilliseconds = SystemClock.uptimeMillis() - startTime;
updatedTime = timeSwapBuff + timeInMilliseconds;
String hms = String.format("%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(updatedTime),
TimeUnit.MILLISECONDS.toMinutes(updatedTime) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(updatedTime)),
TimeUnit.MILLISECONDS.toSeconds(updatedTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(updatedTime)));
timerValue.setText(hms);
customHandler.postDelayed(this, 0);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerValue = findViewById(R.id.timer);
startButton = findViewById(R.id.start);
pauseButton = findViewById(R.id.pause);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
customHandler.postDelayed(updateTimerThread, 0);
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff += timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
}
}
Upvotes: 2
Reputation: 138
I guess the issue is with pause button listener ..... It should be
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwapBuff = timeInMilliseconds;
customHandler.removeCallbacks(updateTimerThread);
}
});
Upvotes: 1