I have an activity that starts after a certain time. Once this activity starts, the user is supposed to press a button that starts a new timer.
However, if the user does not press the button I want a toast to be displayed every 5 seconds until the button is pressed.
I'm trying to use a CountDownTimer. Here is the basics of the code I have so far:
I have no errors but no toasts are being displayed at the moment. Is it ok to have the MyCount class inside the BreakActivity class (if i put it outside I get an error).
Any help in getting this sorted will be greatly appreciated!
public class BreakActivity extends Activity {
Button startBreakButton; // button to start the break timer
Boolean clicked= false;
CountDownTimer counter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakscreen); // sets layout to breakscreen.xml
MyCount counter;
counter=new MyCount(5000,1000);
counter.start();
startBreakButton = (Button) findViewById(R.id.startBreakButton);
startBreakButton.setOnClickListener(mStartListener);
View.OnClickListener mStartListener = new OnClickListener() {
clicked=true;
//other listener code
};
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
if(clicked=false){
Toast.makeText(getApplicationContext(), "TAKE A BREAK", Toast.LENGTH_LONG).show();
counter= new MyCount(5000,1000);
counter.start();
}
}
#Override
public void onTick(long millisUntilFinished) {
long s1 = millisUntilFinished;
}
}
};
change :
if(clicked=false)
to
if(clicked==false)
Related
I'm trying to do some example about countdown timer using Button and set OnclickListener for that Button. My Default value is 10 and it will be decrease each second, how can i reset my value back to 10?
CountDownTimer cannot be restarted, it can only be used once. You either have to create your own count down class that can handle being restarted, or just create a new instance of your CountDownTimer and cancel the old instance.
See the example code below where we have a CountDownTimer that counts down for 10 seconds in 1 second intervals, a Button that resets the timer when clicked (by cancelling the current timer and starting a new one), and a TextView that displays the time left in the current timer.
public class YourActivity extends Activity {
private CountDownTimer countDownTimer;
private TextView timerDisplayTextView;
private static final long TEN_SECONDS = TimeUnit.SECONDS.toMillis(10);
private static final long COUNTDOWN_INTERVAL = TimeUnit.SECONDS.toMillis(1);
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
Button myButton; // initialized here
// timerDisplayTextView initialized here
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
countDownTimer.cancel();
countDownTimer = getNewCountDownTimer(TEN_SECONDS);
countDownTimer.start();
showTimeInTextView(TEN_SECONDS);
}
});
countDownTimer = getNewCountDownTimer(TEN_SECONDS);
countDownTimer.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
countDownTimer.cancel();
}
private void showTimeInTextView(long millisecondsLeft) {
timerDisplayTextView.setText(TimeUnit.MILLISECONDS.toSeconds(millisecondsLeft) + " seconds left");
}
private CountDownTimer getNewCountDownTimer(long length) {
return new CountDownTimer(length, COUNTDOWN_INTERVAL) {
#Override
public void onTick(long millisUntilFinished) {
showTimeInTextView(millisUntilFinished);
}
#Override
public void onFinish() {
}
};
}
}
I am writing an Android application in which I am using timer android. It is working perfectly, now I want to make it like it should vibrate on every second until the completion of the time.
My code is given below:
public class TimerActivity extends Activity {
Button btnStart, btnStop;
TextView textViewTime;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_responders_timer);
btnStop = (Button)findViewById(R.id.CanelButton);
textViewTime = (TextView)findViewById(R.id.textViewTime);
textViewTime.setText("10");
textViewTime.setTextSize(120);
final CounterClass timer = new CounterClass(10000,1000);
textViewTime.setTextSize(120);
timer.start();
btnStop.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
timer.cancel();
}
});
}
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#SuppressLint("NewApi")
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
textViewTime.setTextSize(50);
textViewTime.setText("Completed.");
}
#SuppressLint("NewApi")
#TargetApi(Build.VERSION_CODES.GINGERBREAD)
#Override
public void onTick(long millisUntilFinished) {
textViewTime.setText(""+millisUntilFinished / 1000);
}
}
write
import android.os.Vibrator;
...
Vibrator vibrate = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for "whatever_time_u_want" milliseconds
v.vibrate(whatever_time_u_want);
give permission
<uses-permission android:name="android.permission.VIBRATE"/>
I'm trying to create a mini-game loop in which the user has 10 seconds to click a button or loses the game. When I run the while game loop, I want the while loop to start the timer and then either wait for the timer to run out or the user to click a button. Also, when the code runs, the app crashes within the while loop. Not sure how to continue.
I'm rather new to android.
Thanks in advance.
public class MainGame extends Activity implements OnClickListener {
ProgressBar progress1;
Boolean gameon = true;
Button option1;
Button option2;
Button option3;
private CountDownTimer countDownTimer;
private final long startTime = 10*1000;
private final long interval= 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_game);
Log.d("Mark", "justCreated Completed");
initializebuttons();//also initializes timer, buttons, text, etc.
Log.d("Mark", "Initialize Buttons Function Completed");
maingameloop();
//Drawable draw = getResources().getDrawable(R.drawable.custom_progress_bar);
//progress1.setProgressDrawable(draw);
}
private void initializebuttons(){
option1 = (Button) findViewById(R.id.buttonchoice1);
option1.setOnClickListener(this);
option2 = (Button) findViewById(R.id.buttonchoice2);
option2.setOnClickListener(this);
option3 = (Button) findViewById(R.id.buttonchoice3);
option3.setOnClickListener(this);
countDownTimer = new MyCountDownTimer (startTime, interval);
Log.d("Mark", "Inside the initialize buttons function");
}
private void maingameloop(){
while (gameon){
Log.d("Mark", "while loop running");
countDownTimer.start();
}
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval){
super (startTime, interval);
synchronized(this){
}
}
#Override
public void onFinish() {
option1.setBackgroundColor(getResources().getColor(R.color.LightGreen));
}
#Override
public void onTick(long millisUntilFinished) {
}
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.buttonchoice1:
break;
case R.id.buttonchoice2:
break;
case R.id.buttonchoice3:
default:
break;
}
}
}
With this
while (gameon){
Log.d("Mark", "while loop running");
countDownTimer.start();
}
you will run the code
countDownTimer.start();
with every iteration which I'm sure is not what you want. You don't need (want?) this while loop at all. Simply start the timer whenever you need then in onFinish() run the code that you want to run when time runs out and place this code in the Button click as well. Obviously you could put this all in one function and call that function from onFinish() and onClick() or whatever.
I would also suggest checking in onClick(), or the method which runs the code, to cancel the timer and initialize it to null.
I think I get what you are saying, and if so, try doing this
instead of initializeButtons(), do
countDownTimer = new MyCountDownTimer (startTime, interval);
countDownTimer.start();
and then in onStart() of your CountDownTimer call initializeButtons();
private void initializebuttons(){
option1 = (Button) findViewById(R.id.buttonchoice1);
option1.setOnClickListener(this);
option2 = (Button) findViewById(R.id.buttonchoice2);
option2.setOnClickListener(this);
option3 = (Button) findViewById(R.id.buttonchoice3);
option3.setOnClickListener(this);
Log.d("Mark", "Inside the initialize buttons function");
}
in onClick(View v), assuming that the answer and the text of the button are the same
if(v.getText().toString().equals(answer)){
// do what you would do if you win
win = true; // win is a private class boolean I've created for you
countDownTimer.cancel();
}
and then in onFinish()
if(!win){
// do what you want for a loss
}
This will make it so that you determine if a button is clicked within the limit. If so you win, if not you lose. This isn't exactly based on your code, but can be modified to work.
this is my first question here.
I need to implement in my app six countdown timers running one after another. When first finishes, next one starts and so on. Init time of every single run depends on user input. The problem is that I need to put different code in onTick() and onFinish() methods for every single countdown timer running and, well, I'm not sure how to start a next counter after one is finished. I was thinking about calling next counter in onFinish() method of current one but I can't figure out how to do this with 6 counters.
This is my Countdown timer class:
public class Counter extends CountDownTimer
{
public Counter(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
public void onTick(long millisUntilFinished)
{
//this code is the same for every counter
timer_view.setText(formatTime(millisUntilFinished));
//this code depends on specific counter running
output_view1.setText("My output here");
output_view2.setText("My output here");
output_view3.setText("My output here");
}
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
timerHasStarted = false;
}
}
I'm starting my counter in the same activity:
if(!timerHasStarted)
{
counter = new Counter(user_input1, 1000);
counter.start();
}
You probably need to break out the start timer functionallity and call it in onFinish().
public void startTimer(int counterId){
Counter counter = null;
switch(counterId){
case 0:
counter = new CounterOne(counterId,user_input1,1000);
break;
/* Counter 1-5 goes here*/
default:
break;
}
if(counter !=null ){
counter.start();
}
}
then start your next timer in onFinsh()
public abstract class Counter extends CountDownTimer
{
private int counterId;
public Counter(int counterId /*counter id start with 0*/,long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
this.counterId = counterId;
}
public abstract void onTick(long millisUntilFinished);
public void onFinish()
{
playSound(sound_id_1);
runMyMethod(user_input_1);
startTimer(this.counterId++);
}
}
public class CounterOne extends Counter{
public void onTick(long millisUntilFinished)
{
//counter 1 logic
}
}
/* Subclass others. eg. CounterTwo etc. */
You never set
timerHasStarted
to true. It's always false, so...yeah, one timer after another. Set it to true before calling counter.start() and it should work.
I am a new android developer. Recently, launched a word puzzle app - Wozzle
I want to add timer to the game so that a player gets a limited time to make a move. How to do that ? Also, the timer should be displayed to the player.
Try this,
public class CountDownTest extends Activity {
TextView tv; // textview to display the countdown
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
this.setContentView(tv);
// 5000 is the starting number (in milliseconds)
// 1000 is the number to count down each time (in milliseconds)
MyCount counter = new MyCount(5000, 1000);
counter.start();
}
//countdowntimer is an abstract class, so extend it and fill in methods
public class MyCount extends CountDownTimer{
public MyCount(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
#Override
public void onFinish() {
tv.setText("done!");
}
#Override
public void onTick(long millisUntilFinished) {
tv.setText("Left: " + millisUntilFinished/1000);
}
}
}
Have a look at these links
A Stitch in Time
On screen timer in Android application?
stop watch logic
Android Game Programming: The Game Loop