hey guys what's happening my name is cal and in today's java and android studio tutorial we're
going to be building a very simple calendar view application so it's just going to be a
basic calendar view you know you can go through the months forwards and back we're going to make
sure that monday lines up with the first of each month and we know we've got the correct number
of days in each month and we're going to build it all from scratch there's going to be no frameworks
so hopefully this is a really good building block you could sort of take this calendar
application and then build upon it depending on what you sort of want to use for your app
but yeah i feel like there's a lot of room to build from what we've got here
and customize it to suit your app link in the description to the source code
cool so new android studio project empty activity just got to make sure that the minimum sdk
is version 26 and i'm just calling this app calendar app example and then the first thing
we're going to do is head into our values i'm going to get rid of all these purple colors
and i'm going to add in a color gray i'm also going to add in a color blue and feel free to
copy those hex colors it doesn't really matter what the color is that's just the little color
scheme i've decided to go for i'm going to delete the night theme i'm going to remove everything in
our theme and change it to theme app compact light no action bar and we can head back into our main
xml and i'm just going to change the layout to a linear layout i'm going to set the orientation
to vertical inside that linear layout we're going to create another linear layout this linear layout
is going to have the orientation of horizontal i'm going to give it a little bit of margin to the top
and the bottom so we're going to store our buttons and our year month in this linear layout so i've
just created a new button and i've given it a width of zero dp and we're gonna give it the
weight of one the height is wrap content going to give it a text so the little left sort of arrow
symbol text color is blue background null text style bold and text size 20sp and then copy and
paste that below our text view so that's going to be our right hand button so go to the next month
and we're just going to change our text view up a little bit we're going to give it a width of 0dp
and a weight of 2 and we're going to give it a text size a little bit bigger as well we're going
to say text alignment center text color black just change the text so yeah february 2021 that's
the month that we're recording this video and we're just going to create a couple of on click
listeners so on click methods i'm calling the first one previous month action and the next one
is next month action we're going to give our text view and id so just call it month year tv so month
year text view and then below our linear layout we're going to create another linear layout this
one's going to have a horizontal orientation as well we're going to create a text view inside that
linear layout width of zero dp height wrap content weight one we're gonna give it the text of sunday
so we're gonna have seven of these text views in our current linear layout we're gonna align the
text to the center as well as text size to 16 sp and then copy and paste that down six more times
and just changing the value so monday tuesday wednesday thursday etc and then below that linear
layout we're going to create our recycle view so i've just typed recycler view we're going to give
it an id of calendar recycler view width and height match parent and then i'm just going to
create a new layout resource file i'm going to call it calendar cell so this is for our
recycler view and inside our calendar cell i'm going to create a text view wrap content on the
width and height i'm going to give it an id i'm going to call it cell date text fault text to b1
text size is 20 sp and the text color of black we're going to give our textview some constraint
layout constraints right of right parent left of left parent and that just puts it in the middle
and then we're going to give it vertical bias of 0.25 so it just sits a little bit higher than
the dead center we're going to create a new java class we're going to call it calendar adapter and
then another new java class calling it calendar view holder and our calendar view adapter extends
recycler view adapter and then giving it a type of calendar view holder and then we're going to
implement those methods we just click on the error message and implement the methods and then our
calendar view holder extends recycler view view holder and again just implement the methods and
then we're going to clear a textview our day of month textview inside our calendar view holder
method and say day of month is equal to item view dot find view by id and then just give it our
calendar day text back inside our calendar adapter we're going to declare an array list of strings
we're going to call it days of month and we're just going to create a constructor so just right
click and generate a constructor and then inside our on create view holder we're going to declare
a layout inflator calling it inflator is equal to layout inflator from parent get context and then
we're going to declare a view and we're going to say it's equal to inflator.inflate give it
our calendar view cell parent and attach to root is false we're going to say view group dot layout
params just calling it layout params is equal to view dot get layout params and then we're going to
get the height so we're going to say layout params dot get height is equal to just casting to an int
parent dot get height times 0.166 we're getting the full height of the view and then timing by
0.116 so that we get each cell is exactly 1 6 of the full view we just want to return our new
calendar view holder and passing it in our view in our on bind view holder method we're just going to
say holder dot and we haven't done this yet so i'm just going to head into our calendar view holder
and just make it public and make our text view public as well so that we can access it from
our adapter and say day of month dot set text day of month dot get at position and then in our get
item count method we're just going to return day of month dot size next what we're going to do is
implement an on click listener so i'm just creating an interface i'm calling it on item
listener void on item click passing in a position and a string calling it day text and then we're
going to declare our on-item listener inside our calendar adapter and add that to the constructor
in our calendar view holder we're going to declare a calendar adapter dot on item
listener and same thing we're going to put this in the constructor after that we're going to say
item view set listener we're going to pass it this and we just need to make sure our view holder
implements view on click listener and implement the methods we're going to say on item listener
dot on item click get adapter position and then casting to a string our day of month get text
cool and we're finally ready to head into our main activity java class we're going to declare
a text view calling it monthly text we're going to declare a recycler view calling it calendar
recycle view and we're also going to declare a local date calling it selected date and then
inside our on create we're just going to create a method called init widgets and we're just going
to find both of our views by id so the first one is calendar recycler view we're going to find it
by its id calendar recycler view month year text find view by id giving it month year text view
and then below that we're going to say selected date is equal to local date now so just giving
it the current date and time and then we're going to say set month view and we're going to create
this method and we're just going to pop it below our init widgets method we're going to say month
year text dot set text and we're going to create another method that we're going to give our set
text we're going to call our method month year from date it receives a local date and we're
just going to say date time formatter calling it formatter date time formatter of pattern and our
pattern is going to be the string month and then the year and then we're just going to say return
date dot format giving it the formatter and now we can set our month year text to
our get month year string from our selected date and then we're going to declare an array list of
strings we're going to call it days in month and what we're going to do here is actually create
another method to get our days in month given our selected date so we're going to declare an array
list of strings we're going to call it days in month is equal to a new array list we're going
to declare a year month object calling it year month i'm going to say year month from date i'm
going to declare it int days in month is equal to year month.length of month so get how many days
are in the month here another local date first a month so get whatever the first day of the month
is is equal to selected date with day of month giving it one so get the first day of the month
and then we're going to declare another int we're going to call it day of week which is equal to
first of month dot get day of week get value so that's going to return an integer between 0 and
7. we're going to create a for loop it's going to be an all-star for loop so we're just going to say
for int i is equal to 1 while i is less than or equal to 42 add 1 to i and then we're going to say
if i is less than day of week or i is greater than day of month plus day of week we're going to add
a blank square if it's before the first of the month or if it's after the 31st or the end of the
month add a blank square otherwise i want you to add the value of i plus the day of week and then
we're just going to return our days in month array and then in our set month view we're going to
declare a calendar adapter we're going to call it calendar adapter is equal to new calendar
adapter giving it days of month and our on item listener is this given that we need to make our
main activity implement calendar adapter dot on item listener and then we're just going
to implement that method but we'll come back to that later recycler view layout manager calling
it layout manager is equal to new grid layout manager we're just giving the application context
and our span count is seven so we're going to have seven columns in our recycle view and then
we're going to say calendar recycler view dot set layout manager giving it our layout manager
set the adapter as well on our calendar recycler view giving it our adapter next in our previous
month action we're just going to say selected date is equal to selected date minus a month and then
set our month view again and we're going to do the same thing in next month but instead we're going
to plus a month and then set our month view after that and then in our on item click if our cell
doesn't equal nothing so it's not a blank cell and then we're just going to make a bit of toast and
say you've selected this date so really you could put whatever you like in here but that's just
an example of how you can get the on item click listener working with your recycler view cool and
i didn't like this not being a string resource i've just extracted both of those strings to a
string resource and i also made a mistake i need to make this minus day of week i never
declared selected date is equal to selected day plus month and if we build and run this you can
see we've got a pretty basic calendar view if you click on a date it gives you that little
bit of toast and you can see all the dates should be correct first a month is starting
on the correct day of the month and we even got a leap year there so february 2020 was a leap year
so we can select february 29th that was a very basic calendar view in android studio using java
you