I am trying to show a timer on the screen and on that screen I have a TextView and a RecyclerView.
The RecyclerView contains some Spinners.
When the onTick() method is executed, the Spinners are closed automatically.
Please provide any solution to this problem.
Code of my CountDownTimer
public class CountDownTimerClass extends CountDownTimer {
public CountDownTimerClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
String time = String.format(Locale.US, "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
mTvTimer.setText(time);
}
#Override
public void onFinish() {
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_member_enter_details);
mRecyclerView = (RecyclerView) findViewById(R.id.recycleView);
mTvTimer = (TextView) findViewById(R.id.tv_timer);
mTvBlockNo = (TextView) findViewById(R.id.tv_block_no);
mRlHeader = findViewById(R.id.rlHeader);
//hit for web service. In the response i am starting timer
getData(Event.GET_EXPIRATION_TIME);
}
Pretty sure it's closing because you're calling setText on another view. That may trigger a focus change which would cause the Spinner to close.
You may be able to avoid this by setting focusable to false on the TextView (via XML preferably), or calling clearFocus() on the TextView after setText()
You should update your label or textview's text in the UI Thread, so your code should look like
public void onTick(long millisUntilFinished) {
String time = String.format(Locale.US, "%02d:%02d:%02d",
TimeUnit.MILLISECONDS.toHours(millisUntilFinished),
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)),
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
runOnUiThread(new Runnable() {
#Override
public void run() {
mTvTimer.setText(time);
}
});
}
Related
i wrote this code and i want when i click on btnGetPincode, a 60 sec count down timer start to run.
but it didnt happen and the result in textview= 00:00 and nothing happen. why?
this is my code:
btnGetPinCode.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
btnGetPinCode.setClickable(false);
btnGetPinCode.setBackgroundResource((R.drawable.button4));
txtShowPinCode.setVisibility(View.VISIBLE);
//initialize timer duration
long duration = TimeUnit.MINUTES.toMillis(1);
//initialize timer countdown timer
new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
, TimeUnit.MILLISECONDS.toMinutes(1)
, TimeUnit.MILLISECONDS.toSeconds(1) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(1)));
//set converted time to textView
txtTimer.setVisibility(View.VISIBLE);
txtTimer.setText(duration2+"");
btnOk.setText(duration2);
}
#Override
public void onFinish() {
//when timer finished, hide text view
txtTimer.setVisibility(View.INVISIBLE);
btnGetPinCode.setBackgroundResource(R.drawable.button);
btnGetPinCode.setClickable(true);
}
}.start();
});
}
Issue: You set 1 in each argument in duration2 variable, so the TextView won't ever be changed, you need to set the actual value that is changed on every timer tick which is the parameter of the onTick() method >> millisUntilFinished
Change your timer to:
new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
, TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) //<<<<< change here
, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - //<<<< change here
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); //<<<<< change here
//set converted time to textView
txtTimer.setVisibility(View.VISIBLE);
txtTimer.setText(duration2+"");
btnOk.setText(duration2);
}
#Override
public void onFinish() {
//when timer finished, hide text view
txtTimer.setVisibility(View.INVISIBLE);
btnGetPinCode.setBackgroundResource(R.drawable.button);
btnGetPinCode.setClickable(true);
}
}.start();
The main problem lies in calculation of time duration2
//Declare timer
CountDownTimer cTimer = null;
cTimer = new CountDownTimer(duration, 1000) {
#Override
public void onTick(long millisUntilFinished) {
String duration2 = String.format(Locale.ENGLISH, "%02d : %02d"
, TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) //Here is the problem
, TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - //Here is the problem
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))); //<<<<< change here
//set converted time to textView
txtTimer.setVisibility(View.VISIBLE);
txtTimer.setText(duration2+"");
btnOk.setText(duration2);
}
#Override
public void onFinish() {
//when timer finished, hide text view
txtTimer.setVisibility(View.INVISIBLE);
btnGetPinCode.setBackgroundResource(R.drawable.button);
btnGetPinCode.setClickable(true);
}
}.start();
You should also need to clean up the references so as to prevent memory leaks. This needs to be called cTtimer.cancel() whenever the onDestroy()/onDestroyView() in the owning Activity/Fragment is called.
void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}
I want to build a 5 second timer that counts down to 0 in 1 second intervils and then resets to the initial value of 5 seconds. The timer needs to run continously. After looking at this thread,
Android - loop part of the code every 5 seconds
I used the CountDownTimer Class and called the start() method within the onFinish() method so that when the timer finished, it would reset to 5. It does run on a continous loop, however I notice that after the first loop, it either countsdown as 4-3-2-1-0 or 5-3-2-1-0.
Can somebody explain to me why this happens?
private long START_TIME_IN_MILLIS = 5000;
//set up our variables
private CountDownTimer timer;
private TextView textView;
private Button starttimer;
private long mTimeLeftInMillis = START_TIME_IN_MILLIS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.text_view_countdown);
starttimer = findViewById(R.id.button_start_pause);
//set onclik listener when touch imput button
starttimer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
beginTime();
}
});
}
//creating my own method
private void beginTime() {
timer = new CountDownTimer(mTimeLeftInMillis, 1000) {
#Override
public void onTick(long millisUntilFinished) {
mTimeLeftInMillis = millisUntilFinished;
updateCountDownText();
}
#Override
public void onFinish() {
start();
}
}.start();
}
private void updateCountDownText(){
int minutes= (int) (mTimeLeftInMillis / 1000)/ 60;
int seconds= (int) (mTimeLeftInMillis / 1000) % 60;
String timeLeftFormatted = String.format(Locale.getDefault(),"%02d:%02d", minutes, seconds);
textView.setText(timeLeftFormatted);
}
}
Try this
#Override
public void onFinish() {
mTimeLeftInMillis = START_TIME_IN_MILLIS;
}
I'm new to Android development and currently trying to implement a very simple countdown timer, which shall keep on counting after reaching 00:00, but having the text color in red when below 00:00.
I have two problems:
1) It starts counting from one second less than what I set (in the example it starts at 00:09 instead of at 00:10).
2) After it reaches 00:00, on the next tick it turns the text red but keeps the counter on 00:00 (this latter part is not intended), then continue counting as -00:01, -00:02, etc. So it counts twice on 00:00, once in white, once in red. Any ideas why this occurs? Here's my code:
public class myTimerActivity extends Activity {
Chronometer mainTimer;
long mStartValue = 10; // Countdown value
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface for this Activity
setContentView(R.layout.activity_mytimer);
// Initialize Chronometer object
mainTimer = (Chronometer) findViewById(R.id.chronometer);
mainTimer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
#Override
public void onChronometerTick(Chronometer chronometer) {
if (SystemClock.elapsedRealtime()>=chronometer.getBase()) {
chronometer.setTextColor(Color.parseColor("#FF0000"));
};
}
});
mainTimer.setFormat("%s");
mainTimer.setBase(SystemClock.elapsedRealtime()+mStartValue*1000);
mainTimer.start();
}
...
}
UPDATE:
Here's another implementation, as suggested by Roger:
public class myTimerActivity extends Activity {
long mStartValue = 10; // Countdown value
private TextView txtCounter;
private static final String FORMAT_COUNTER = "%02d:%02d:%02d";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the user interface for this Activity
setContentView(R.layout.activity_mytimer);
txtCounter = (TextView)findViewById(R.id.mTextField);
new CountDownTimer(mStartValue*1000, 1000) {
public void onTick(long millisUntilFinished) {
txtCounter.setText("" + String.format(FORMAT_COUNTER,
TimeUnit.MILLISECONDS.toHours(millisUntilFinished), // HOURS PASSED
TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millisUntilFinished)), // MINUTES PASSED (over the hours)
TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) // SECONDS PASSED (over the minutes)
));
}
public void onFinish() {
txtCounter.setTextColor(Color.parseColor("#FF0000"));
}
}.start();
}
...
}
I'm trying to implement a countdown widget for my application. I need help in writing the code in such a way that it starts with a button and automatically stops when the time is up which would then show a text,"Finished". Any help is greatly appreciated ! Thank You in Advance !
I have actually tried some code examples. Gradle is able to run successfully without any problems. However, when I hit the start button, nothing happens. Here are the codes that I used:
public class countdown extends AppCompatActivity {
Button buttonStart;
TextView textCounter;
MyCountDownTimer myCountDownTimer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStart = (Button)findViewById(R.id.start);
textCounter = (TextView)findViewById(R.id.counter);
buttonStart.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(30000, 1000);
myCountDownTimer.start();
}
});
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onTick(long millisUntilFinished) {
textCounter.setText("seconds remaining: " + millisUntilFinished / 1000);
}
#Override
public void onFinish() {
textCounter.setText("Finished!");
}
}}
Put this code in your button click listener:
new CountDownTimer(30000, 1000) {//CountDownTimer(edittext1.getText()+edittext2.getText()) also parse it to long
public void onTick(long millisUntilFinished) {
mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
mTextField.setText("finished!");
}
}
.start();
Refer to this link.
I'm trying to pass the countdowntimer a variable and use that variable as the amount of milliseconds to count down. If I simply enter the value the countdown works properly, but if I pass it a long variable it just runs the onFinish function.
Here's the actual code:
public CountDownTimer countDown = new CountDownTimer(respawnTime, 1000) {
#Override
public void onTick(long millisUntilFinished) {
timer =(Integer)(int) millisUntilFinished / 1000;
if(timer < 31)
timerText.setTextColor(Color.parseColor("#FF0000"));
timerText.setText(timer.toString());
}
#Override
public void onFinish() {
timerText.setTextColor(Color.parseColor("#00FF00"));
timerText.setText("UP");
}
};
At this point I have respawnTime set to equal 360000 hoping for a 360 second countdown, but like I said it just immediately runs the onFinish. Simply changing the first parameter to a literal instead of a variable fixes everything but I need to use a variable here. Thanks in advance for the help!
Change
#Override
public void onTick(long millisUntilFinished) {
to
#Override
public void onTick(long respawnTime) {
use the variable you are sending in the constructor in onTick().
Edit
Here is one of mine
private class MyCountDown extends CountDownTimer
{
long duration, interval;
public MyCountDown(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
duration = millisInFuture;
interval = countDownInterval;
start();
}
#Override
public void onFinish() {
secs = 10;
Intent intent = new Intent(CalibrationTimeoutScreen.this, CalibrationTakeTempScreen.class);
intent.addCategory(Intent.CATEGORY_DEFAULT);
startActivity(intent);
CalibrationTimeoutScreen.this.finish();
}
#Override
public void onTick(long duration) {
cd.setText(String.valueOf(secs));
secs = secs - 1;
}
}
You did not run start(). Simply add .start() after the last '}' or add a new line calling countDown.start().