Android Countdown timer, continue timer on new activity - java

I've created a countdowntimer that works but it starts again on each activity. I'm trying to save the countdown value and use it in the second activity. Below is the countdown code on my main activity page.
//Countdown Start
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
final int j = (int) millisUntilFinished / 1000;
long timed;
timed = millisUntilFinished;
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText("" + (millisUntilFinished));
runOnUiThread(new Runnable() {
#Override
public void run() {
TextView textic = (TextView) findViewById(R.id.textView2);
textic.setText(Integer.toString((j)));;
}
});
}
public void onFinish() {
}
}.start();
//Countdown Finish
I've tried storing the countdown value and then using passextra in my intent to pass "timed" but that didn't work. I've searched around and can't find any solution that works. Any help would be greatly appreciated.
Thanks
================
EDIT
================
public class MainActivity extends startscreen{
private CountDownTimer timer3;
public void onCreate(){
timer3 = new CountDownTimer(10000,100){
public void onTick(long millisUntilFinished) {
TextView displaytime = (TextView) findViewById(R.id.textView3);
displaytime.setText("" + (millisUntilFinished));}
public void onFinish() {
}
}.start();
};;
public void startTimer(){
}
public void cancelTimer(){
}
==============
EDIT 2
//Counter 1
Counter1 = new CountDownTimer(10000 , 1000) {
public void onTick(long millisUntilFinished) {
mCounter1TextField.setText("Seconds left: " + formatTime(millisUntilFinished));
}
public void onFinish() {
mCounter1TextField.setText("Finished!");
// Counter1.start();
}
};
Counter1.start();
public void starttimer() {
Counter1.start();
}
And then in my second activity i've got:
MainActivity Counter1 = (MainActivity) getApplicationContext();
Counter1.starttimer();
TextView mCounter1TextField=(TextView)findViewById(R.id.textView4);
mCounter1TextField.setText("Seconds left: " + (Counter1));

Extend the Application class and add your global timer there. Your class would look something like this
public class MyApplication extends Application{
private CountDownTimer timer;
public void onCreate(){
super.onCreate();
timer = new CountDownTimer(10000,100){
.....
};
}
public void startTimer(){
...
}
public void cancelTimer(){
...
}
}
Start this timer by making a call to getApplicationContext() from an activity
MyApplication application = (MyApplication) getApplicationContext();
application.startTimer();
Also, make sure you rename your application in your Android manifest.
<application ... android:name=".MyApplication" .../>

I would suggest either writing the CountDownTimer in its own class and extend it as a Service and then start the service when you want to start the timer, or make it a Singleton so all activities access the same timer.

Related

Get variable from countdown and set it to TextView

I need your help to get a variable from the countdown and set that value to text view. Suppose if countdown stopped at 0:40 sec and I want to put that number to text view.
So I'm using Seekbar to update the time with progress and a textview. And suppose I stopped at a certain number, next time I start again, let the number start from when it stopped. I have uploaded the image of output. Thanks
I learned to create this app from udemy online tutorial. Its called Complete android developer course- Build 23 Apps!!. Its lesson 38- App Egg timer.
This is my MainActivity.java file
public class MainActivity extends AppCompatActivity {
TextView timerTv;
SeekBar timerSeekBar;
Button startBtn;
CountDownTimer countDownTimer;
boolean counterisActive = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timerTv = (TextView) findViewById(R.id.countdowntimertextview);
timerSeekBar = (SeekBar) findViewById(R.id.timerSeekBar);
startBtn = (Button) findViewById(R.id.startBtn);
timerSeekBar.setMax(300);
timerSeekBar.setProgress(20);
timerSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
updateTimer(progress);
Log.i("Seekbar changes", String.valueOf(progress), null);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
public void resetTimer(){
//This is where I want to set the text.
timerTv.setText("Trying to get text from countdown!!");
startBtn.setText("START!");
timerSeekBar.setEnabled(true);
countDownTimer.cancel();
timerSeekBar.setProgress(20);
counterisActive = false;
}
public void buttonClicked(View view){
if(counterisActive){
resetTimer();
}else {
counterisActive = true;
timerSeekBar.setEnabled(false);
startBtn.setText("STOP!");
Log.i("Button Clicked", "Clicked");
countDownTimer = new CountDownTimer(timerSeekBar.getProgress() * 1000 + 100, 1000) {
#Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished / 1000);
}
#Override
public void onFinish() {
MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.templebell);
mediaPlayer.start();
Log.i("Timer Finished", "Done!!");
resetTimer();
}
}.start();
}
}
public void updateTimer(int secondLefts){
int minutes = secondLefts / 60;
int seconds = secondLefts - (minutes * 60);
String secondString = Integer.toString(seconds);
if(seconds <= 9) {
secondString = "0" + seconds;
}
timerTv.setText(minutes + " : " + secondString );
}
}
I think you need to pause the timer.
First create a global long variable in your activity;
long timerProgress;
Change your restProgress() method like below, or you can add new method pauseTimer().
public void restTimer(){
//This is where I want to set the text.
updateTimer((int) timerProgress/1000);
startBtn.setText("START!");
timerSeekBar.setEnabled(true);
countDownTimer.cancel();
timerSeekBar.setProgress((int) timerProgress/1000);
counterisActive = false;
}
Know add this line to your override method onTick.
#Override
public void onTick(long millisUntilFinished) {
updateTimer((int) millisUntilFinished / 1000);
// add this line for store progress timer.
timerProgress = millisUntilFinished;
}
You can add another button one for pause and other for rest Timer.
Try replacing timerTv.setText(minutes + " : " + secondString ); with
runOnUiThread(new Runnable() {
#Override
public void run() {
timerTv.setText(minutes + " : " + secondString );
}
});
If you try to update the UI mid-thread, it will wait until the current thread has finished before updating the text.

Why value of only last iteration(999) is printed in the text field ? I want all numbers to be printed before getting replaced by next number

I am new to android studio and want to overcome from this problem.
It gonna help me a lot while creating some future apps like , stopwatch, timer etc etc.
Thanks in advance !!
public class MainActivity extends AppCompatActivity {
private EditText k;
private Button start;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
k=findViewById(R.id.kf);
start=findViewById(R.id.startf);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=1;i<1000;i++)
{
k.setText(String.valueOf(i) );
}
}
});
}
}
As #f1sh already mentioned in the comments your for loop is executing at such a speed that all you see is the final value. For such cases in android one of the best solutions is to make use of Handler for posting delayed functions without blocking the UI.
So for showing 1 to 999 you can try something like this:
public class MainActivity extends AppCompatActivity {
private EditText k;
private Button start;
int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
k=findViewById(R.id.kf);
start=findViewById(R.id.startf);
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
if (count < 1000) {
k.setText(String.valueOf(count));
count++;
handler.postDelayed(this, 1000);
} else {
handler.removeCallbacks(this);
}
}
};
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count = 1;
handler.postDelayed(runnable, 0);
}
});
}
}
this will keep changing the text in TextView with a 1 second delay, you can change the delay as needed by setting the milliseconds in runnable.
You can even use a countdown timer for this purpose but its more like a workaround and requires you to calculate the correct time etc.
For example displaying 1 to 10 would be something like this:
...
count = 1;
new CountDownTimer(11000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText(String.valueOf(count));
count++;
}
public void onFinish() {
}
}.start();
here you are displaying the value every 1 second for 11 seconds

Set Visibility after countdown ends

Despite thorough search of other user's questions, I don't seem to understand how to do a countdown timer. All I wanna do is setting visibility to GONE after 30 seconds without touching the screen. So far I've done this:
public class StatusFragment extends Fragment {
CountDownTimer countDownTimer;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
}
public void onFinish() {
//SET VISIBILITY TO GONE
}
}.start();
cpHover.setOnClickListener(new OnClickListener() {
//Should I countDownTimer.start()? It says there's an error
#Override
public void onClick(View view) {
if(textBox.getVisibility()==View.GONE){
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
else if(textBox.getVisibility()==View.VISIBLE){
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
});
The process should be quite easy. Setting in onCreate() a 30-second countdown that starts every time the user clicks cpHover. When they click it again, it should restart the countdown. There are two ways of hiding the UI: clicking on the screen when it's visible or not clicking at all in 30+ seconds.
Thanks in advance.
add this inside your onCreate()
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//set visibility as gone here
if(textBox.getVisibility()==View.VISIBLE){
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
}, 30000);
Its very Simple, just change your code as per below :
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View. VISIBLE);
}
public void onFinish() {
//SET VISIBILITY TO GONE
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}
}.start();
you want to start CountDownTimer in Click event then add above code in the click event
Happy Coading....
Update your code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setUpCountDown();
cpHover.setOnClickListener(new OnClickListener() {
//Should I countDownTimer.start()? It says there's an error
#Override
public void onClick(View view) {
if(countDownTimer != null) {
countDownTimer.cancel();
}
setUpCountDown();
}
);
}
private void setUpCountDown() {
countDownTimer = new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
//SET VISIBILITY TO VISIBLE
donutProgress.setVisibility(View.VISIBLE);
textBox.setVisibility(View.VISIBLE);
image.setVisibility(View.VISIBLE);
}
public void onFinish() {
donutProgress.setVisibility(View.GONE);
textBox.setVisibility(View.GONE);
image.setVisibility(View.GONE);
}
}.start();
}

How can i Reset my Timer value meanwhile completion of timer?

I am trying to start a timer when activity created and be able to reset the timer back from zero if the same button is pressed but every time I press the button that initiates the set Interval it seems to be creating a new interval and not resetting the one that was already creating. Can someone help please? Here is my code
timer = (TextView) findViewById(R.id.timer_value);
Count = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("OUT OF TIME!");
if (time < 10000) {
time = 10000;
}
AlertDialog.Builder builder = new AlertDialog.Builder(
TicTacToe.this);
builder.setMessage("You are Out of Time").setPositiveButton(
"Replay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// reset the game environment.
Count.onFinish();
// Count.cancel();
Count.start();
new_game(player_name_1);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}.start();
you can use timers
//in global
Timer myTimer
/**
* its kills runnable
*/
public void stopTimer(){
//handler.removeCallbacks(null); //it resets all timer which handler holds
myTimer.cancel();
}
public void setTimer(int time){//give it 5 for 5 secs
final Runnable Update = new Runnable() {
public void run() {
//do sth
}
};
myTimer = new Timer();
myTimerTimer.schedule(new TimerTask() {
#Override
public void run() {
handler.post(Update);
}
}, 1000, time*1000);
}
If you want to restart your timer, remove Count.onFinish();
Update:
I replaced AlertDialog with a new thread and here is the code that works on my emulator
public class MainActivity extends Activity
{
int time = 10000;
CountDownTimer Count;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView timer = (TextView) findViewById(R.id.timer);
Count = new CountDownTimer(time, 1000) {
public void onTick(long millisUntilFinished) {
timer.setText("Time Left: " + millisUntilFinished / 1000);
}
public void onFinish() {
timer.setText("OUT OF TIME!");
if (time < 10000) {
time = 100000;
}
}
}.start();
}
public void buttonClicked(View view)
{
Log.i("Timer", "Resetting timer");
Count.cancel();
Count.start();
}
}

Android Thread for a timer

public class MainActivity extends Activity
{
int min, sec;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
min = 5;
sec = 0;
final TextView timer1 = (TextView) findViewById(R.id.timer1);
timer1.setText(min + ":" + sec);
Thread t = new Thread() {
public void run() {
sec-=1;
if (sec<0) {
min-=1;
sec=59;
}
timer1.setText(min + ":" + sec);
try
{
sleep(1000);
}
catch (InterruptedException e)
{}
}
};
t.start();
}
}
This is a code for a Thread in Java but it doesn't work. Can you help me?
Its a Timer that counts down from 5 Minutes to 0:00.
In your case you are using threads. So you cannot update ui from the thread other than the ui thread. SO you use runOnUithread. I would suggest you to use a countdown timer or a Handler.
1.CountDownTimer
http://developer.android.com/reference/android/os/CountDownTimer.html
Here's a link to another example. Suggest you to check the link for the count down timer.
Countdowntimer in minutes and seconds
Example:
public class MainActivity extends Activity {
Button b;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.textView1);
b= (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startTimer(200000);
}
});
}
private void startTimer(long time){
CountDownTimer counter = new CountDownTimer(30000, 1000){
public void onTick(long millisUntilDone){
Log.d("counter_label", "Counter text should be changed");
tv.setText("You have " + millisUntilDone + "ms");
}
public void onFinish() {
tv.setText("DONE!");
}
}.start();
}
}
2.You can use a Handler
Example :
Handler m_handler;
Runnable m_handlerTask ;
int timeleft=100;
m_handler = new Handler();
m_handlerTask = new Runnable()
{
#Override
public void run() {
if(timeleft>=0)
{
// do stuff
Log.i("timeleft",""+timeleft);
timeleft--;
}
else
{
m_handler.removeCallbacks(m_handlerTask); // cancel run
}
m_handler.postDelayed(m_handlerTask, 1000);
}
};
m_handlerTask.run();
3.Timer
Timer runs on a different thread. You should update ui on the ui thread. use runOnUiThread
Example :
int timeleft=100;
Timer _t = new Timer();
_t.scheduleAtFixedRate( new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() //run on ui thread
{
public void run()
{
Log.i("timeleft",""+timeleft);
//update ui
}
});
if(timeleft>==0)
{
timeleft--;
}
else
{
_t.cancel();
}
}
}, 1000, 1000 );
You are trying to update the UI Thread from a background Thread with
timer1.setText(
which you can't do. You need to use runOnUiThread(), AsyncTask, CountDownTimer, or something similar.
See this answer for an example of runOnUiThread()
But CountDownTimer is nice for things like this.
Also, when posting a question on SO, statements like "it doesn't work." are very vague and often unhelpful. Please indicate the expected results compared to actual results of your code and logcat if the app is crashing.

Categories

Resources