hey guys how's it going uh today we're going to be building a simple timer app
in android studio using java there's a link in the description to the source
code as always so we're just going to have a basic
window with a stop start button a reset button and the timer um you're
gonna be able to start and stop the timer it's gonna count up from zero
to infinity maybe not infinity but anyway there's gonna be a reset button
we're gonna do a bit of validation so make sure that user really does want to
reset the button so yeah when they hit the reset button
we'll show them a little alert dialog to say
do you really want to reset the timer so i'll show you how to do that
and yeah we're just going to build it all it's going to look a little bit
something like this so yeah stick around
cool so i'm just starting a new android studio project i'm naming this one
timer and then i'm just going to go get my layout sorted i'm just going to
change this to a linear layout
and then i'm just going to put in some colors so i'm just putting in red
green gray this isn't necessary but it just makes it all look a little bit
nicer and then i'm going to get rid of this
text view here and i'm going to put in a toolbar
and i'm just going to say width match parent height wrap content
giving it my background color gray and then in my toolbar i'm going to give
it a text view just name it timer
and i'm giving it black layout gravity center
and give it a nice big text size there of 30 sp
and then i'm going to have a button this button is going to be called reset
and giving it null background it's going to be red and
30sp as well so nice and big and then i'm going to do another button
so this is going to be the stop start button
um so i'm just calling it start for now but that's going to toggle
later on see i'm just going to start it's going to be green
positive kind of color and same text size 30 sp
and i'm going to set the gravity to the end there and i'm just giving it a
little bit of margin make it look a little bit nicer
there
cool and then we just want another text view um
this textview uh even bigger so this is going to
contain our actual time
giving it the color of black
and just putting in the middle i realized i need to give the orientation
vertical for that to actually show up so that's what i've done there
and margin at the top is 40dp cool so i think that's all of our ui
done next i'm just going to set some on click
listeners so reset tapped and start stop tapped
and just opening them up there and then i'm going to give my buttons an
id so i've called this one start stop button
and i'm going to give the textview timer text as its id
should have given that a lowercase there we go timer text
cool so i'm just going to do a little bit of setup to start um
just going to set our timer text text view
and our stop start button just going to declare them
and find them by their layouts so if you've not done this before it gets
pretty repetitive android studio um programming finding views by id but um
yeah just follow along here so yeah see i found that timer text by
that id and then i'm going to just declare a
boolean i have called it time has started and i'm going to give
it equal to false and then in our start stop tapped
um if the time has started is false i'm going to set it to true
and set the text to be stopped otherwise if the timer
hasn't started i'm going to set the timer decided to false and the stop
starting button i'm going to set it to start so you get
how that works it's kind of like a toggle really
um i'm just saying the text color here make it all a little bit prettier i
always found that this set text color was pretty annoying have
to do the context compact get color R.color
it just seems like so over the top but anyway that's that's something for
another day um yeah so set that to red and set that
to green on the toggle and then i'm going to declare my timer
and i'm just going to say timer is equal to new timer
and i'm going to declare our timer task and a double
time and just giving it to zero to start with cool and in our start stop tapped
i'm just going to create a new method called start timer
and create that down there and i'm just going to say timer task is
equal to new timer task and just auto complete that
over override method there that run and then each time it gets run i'm just
going to say time plus 1 and then i'm going to set
the text to so we're actually going to create a
new method called get timer text and that's going
to return a string
and i'm just going to declare an int i'm rounding our double
um to an int so just rounding it off
and i'm going to declare a seconds and this is the bit of funky maths that i
basically just looked at stack overflow how to do this bit
but basically with a full time uh this is how you get the seconds you
can you can look the math up for yourself but
i don't know i just kind of go oh it works so well so yeah
you can see uh the different maths for hours minutes and seconds and then i'm
just going to create another method and this is going
to format our time into our timer string so
we just give it the seconds the minutes and the hours and i'm just saying
string format that int to two decimal places
and i'm going to give it the hours give it a bit of space
and the little two dots what are they called i don't know it doesn't matter
those two little dots and then minutes and seconds so let me
just back up to below our run method i'm just going to say timer dot schedule
at fixed rate i'm going to give it the timer task i'm
going to give a delay of zero so we want it to start straight away
and then the period is a thousand and i believe it's in milliseconds so
a thousand milliseconds is one second cool so a couple of things i just forgot
to do in our else statement here i just need to say timertask.cancel
and then in our run i need to put it in this thread so run on the ui thread
and threads are just basically a way to multitask better so if you've got
something really uh intensive you want to do it on a
background thread but this is just a simple timer so we're
just going to do it on the main ui thread
cool so next we're going to get our um reset button working
so i'm actually going to do an alert dialogue for this so that
it prompts the user to make sure they really do want to reset
the timer. i think it's just good practice so
we're just going to say alert dialog.builder calling it reset alert
giving a context of this i'm going to set the title to reset timer
i'm setting the message here are you sure you want to reset
timer and i'm just going to set a positive button here i'm going to give
it a title of reset and then i'm just going to say new
dialog interface dot on click listener and just auto
complete that override onclick method so i'm just going to copy
and paste that set positive button method
uh just changing it to set neutral button
and change the text there to cancel and then on our click
of our cancel we actually just want to do nothing but if we click on our reset
button i'm just going to check to see if the
timer task is null and i'm just going to say
timertask.cancel next i want to set our start stop button to start and
give it the color of green so i'm actually just going to go
refactor this into a method because i'm going to call it twice
um so i've called that set button ui and i'm just going to change that into color
there because that makes a bit more sense
and then we can just copy and paste this method call
we're going to reset our time back to zero time has started
is equal to false and we're going to say timer text
dot set text and we're going to format the time to 0 0 0. so that's calling
that bottom method we created and finally i'm just going to call show
on our reset alert cool and we should be able to run this
and you can see um yeah we've got our timer here
you can hit start stop stopped it at five seconds
and start it again and then i'm going to say reset and it prompts you
are you sure you want to reset the timer we reset it you see it went back to zero
there and just one more test i want to do i
want to see that my hours and minutes are working correctly so i'm changing
this period to one and i've run that again and you can see
it's quickly going up quite fast and there you go one hour
and thirty faster than i can say so yeah i hope you enjoyed the video
um remember to like and subscribe uh all that jazz
and i'll catch you guys in the next one
you