Using Ascending Timers in Android Studio

Using Ascending Timers in Android StudioEvan LiuBlockedUnblockFollowFollowingJan 15The use of countdown timers in mobile apps and computer programs adds an extra sense of purpose to games that would otherwise lack the pressing time element, or utilities that would otherwise lack the moderation to perform adequately.

By adding a few lines of code to your Android Studio game or app, you can use timers as well!Parts of a TimerThe timer explained in this tutorial operates primarily using the Runnable interface and Handler objects.

Together, these components are able to send and process information stored by the Runnable at specified time intervals, making it possible to continuously pass a new time every second even after running the project only once.

Before adding any Runnables or Handlers, however, we first have to lay out the foundation of the timer code.

To begin, we can initialize an integer instance variable called startTime; this variable will help maintain the accuracy of the timer when it inevitably runs.

Also, it is important to include a TextView instance variable, because this timer relies on setting the text within the TextView to the time.

With instance variables out of the way, we can turn to the onCreate( ) method.

Here is a picture of the completed method.

The most notable changes to our onCreate( ) are the defining of our startTime variable and the line that follows it that acts on the Handler object called timerHandler.

This is the handler that we will eventually use to pass the time from our Runnable, later defined as timerRunnable.

We also included a wireWidgets( ) method, linking our TextView instance variable to a TextView in the project’s design layout, so that we may operate on the TextView directly.

The value that we initialized startTime to is an ever-changing value invoked by System.

currentTimeMillis( ).

For whatever reason, this method returns a value equal to the number of milliseconds since the midnight of January 1, 1970.

While this number may seem unnecessary when creating a timer, it becomes especially useful when the timer starts.

Here is a picture of the remainder of the timer code, including the calculation aspect, as well as the wireWidgets( ) method mentioned earlier.

Here we can finally use startTime to ensure that the timer begins counting immediately when the project starts.

It also allows the timer to change its value while the project is running, instead of remaining at a single time.

The variable whose value is dependent upon the startTime is the long variable mills, which keeps track of the number of milliseconds since the number of milliseconds SINCE January 1, 1970.

From this number of milliseconds, we are able to calculate its translation to seconds, and then minutes, naming the variables respectively and defining them as integers.

The reason that we are using integers instead of longs or doubles to name these variables is that this timer will be running in real time, and it would be super weird to see a timer at 5.

8723234890 seconds and 1.

359784652778956 minutes, visibly scaling up by an absurd number of milliseconds every second.

Next, to ensure that the value of seconds doesn’t ever exceed 60, we make sure to define the variable again as (seconds % 60), so that upon hitting 60 seconds, the displayed number of seconds will reset to 0 and continue as usual.

Now we can move on to the timer’s display and how to actually start it.

We can set the time to the textView the same way we normally would, using .

setText(), to which we can also pass specific formatting using format( ).

For the format, we pass in the argument “%d:%02d”, which allows the number of seconds to be printed with a zero in front of it if it is a single digit number.

The following line invokes the same method as in our onCreate( ), postDelayed( ), which adds the timer to the list of actions to be performed by the activity.

With that done, our timer is complete!.Here is what the finished product looks like!This explanation may be limited to exploring ascending timers, but the process for creating descending timers might be even less complicated.

The developers of android included an object called CountDownTimer( ), which takes an argument that expresses the desired amount of run time in milliseconds, and an argument that specifies the interval at which the time increments.

Here is a quick example of how the code for a descending timer might work, and a demonstration of how it might look…private void runCountdown() { new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { testTimer.

setText("seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { testTimer.

setText("done!"); } }.

start();}Thanks for listening.

. More details

Leave a Reply