0

In my app I have a countdown which counts from five to one. At the moment, the counter starts as soon as the activity is started. What I want is to stay at 5 seconds and wait to count down until the screen is touched. So the countdown timer should be started which a touch event.

    public class MainActivity extends Activity implements OnGestureListener {
    private static final String FORMAT = "%02d:%02d";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);

        new CountDownTimer(5000, 10) {
            public void onTick(long millisUntilFinished) {
                text.setText("" + String.format("%02d:%03d",
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
                ));

                if (animationRunning) {
                    cancel();
                }
            }

            public void onFinish() {
                text.setText("done!");
            }
        }.start();
    }


    @Override
    public boolean onTouchEvent(MotionEvent touchevent) {

    }
}
Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
jle
  • 686
  • 1
  • 8
  • 24

2 Answers2

1

Place your count down timer inside onTouch like below

public class MainActivity extends Activity implements OnGestureListener {
    private static final String FORMAT = "%02d:%02d";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);


    }


    @Override
    public boolean onTouchEvent(MotionEvent touchevent) {
        new CountDownTimer(5000, 10) {
            public void onTick(long millisUntilFinished) {
                text.setText("" + String.format("%02d:%03d",
                        TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
                        TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
                ));

                if (animationRunning) {
                    cancel();
                }
            }

            public void onFinish() {
                text.setText("done!");
            }
        }.start();
    }
}
Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
Manohar
  • 22,116
  • 9
  • 108
  • 144
  • Okay, that makes sense. and what would I do if the countdown is part of a method? – jle Dec 24 '16 at 10:53
  • call your method inside onTouchEvent or create a new countdown timer for onTouchEvent :) – Manohar Dec 24 '16 at 10:55
  • So is there no way, to 'pause' the activity until the screen is touched for the first time the app is started? – jle Dec 24 '16 at 10:56
  • place all your code inside a method and call that method inside onTouchEvent – Manohar Dec 24 '16 at 10:57
  • if you only want it for first time then you have to save your first launch inside shared prefs, please accept the answer if helped :) – Manohar Dec 24 '16 at 10:59
  • Can you specify how to do that? I'll do - the post is too young to accept an answer :) – jle Dec 24 '16 at 11:01
  • refer http://stackoverflow.com/a/7217834/6478047 ,you can also refer http://stackoverflow.com/questions/4636141/determine-if-android-app-is-the-first-time-used – Manohar Dec 24 '16 at 11:03
  • Are shared preferences only for starting the first time at all, or every time the app is re-opened? – jle Dec 24 '16 at 11:35
  • Only for first time, there is no need to save any thing for app reopened – Manohar Dec 24 '16 at 11:42
  • No, but I'm creating an app and because it's on time the user shouldn't be need to start immediately after the activity is started, but when he taps to 'confirm' to start – jle Dec 24 '16 at 11:45
  • write all code in a method and call that method inside onclickListner of confirm , no need to use shared preferences – Manohar Dec 24 '16 at 11:48
  • Thanks, you brought me to an idea! – jle Dec 24 '16 at 11:59
0

Move this section of code to your onTouchEvent, because it's on activity create that is why its starting when your activity starts

new CountDownTimer(5000, 10) {
    public void onTick(long millisUntilFinished) {
        text.setText("" + String.format("%02d:%03d",
                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)),
                TimeUnit.MILLISECONDS.toMillis(millisUntilFinished) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished))
        ));

        if (animationRunning) {
            cancel();
        }
    }

    public void onFinish() {
        text.setText("done!");
    }
}.start();

you should be fine

Sasi Kathimanda
  • 1,788
  • 3
  • 25
  • 47
Babajide Apata
  • 671
  • 6
  • 18