Calendar App Example Android Studio Tutorial

In Mobile App Development


Calendar App Example Android Studio Tutorial - read the full article about app development tutorial, Mobile App Development and Native and cross-platform solutions from Code With Cal on Qualified.One
alt
Code With Cal
Youtube Blogger
alt

hey guys whats happening my name is cal and in  todays java and android studio tutorial were going to be building a very simple calendar  view application so its just going to be a basic calendar view you know you can go through  the months forwards and back were going to make sure that monday lines up with the first of each  month and we know weve got the correct number of days in each month and were going to build it  all from scratch theres 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 theres a lot of  room to build from what weve 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 im just calling this app  calendar app example and then the first thing were going to do is head into our values im  going to get rid of all these purple colors and im going to add in a color gray im also  going to add in a color blue and feel free to copy those hex colors it doesnt really matter  what the color is thats just the little color scheme ive decided to go for im going to delete  the night theme im 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 im just going to change the layout to  a linear layout im going to set the orientation to vertical inside that linear layout were going  to create another linear layout this linear layout is going to have the orientation of horizontal im  going to give it a little bit of margin to the top and the bottom so were going to store our buttons  and our year month in this linear layout so ive just created a new button and ive given it a  width of zero dp and were 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 thats going to  be our right hand button so go to the next month and were just going to change our text view up a  little bit were going to give it a width of 0dp and a weight of 2 and were going to give it a  text size a little bit bigger as well were going to say text alignment center text color black  just change the text so yeah february 2021 thats the month that were recording this video and  were just going to create a couple of on click listeners so on click methods im calling the  first one previous month action and the next one is next month action were 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  were going to create another linear layout this ones going to have a horizontal orientation as  well were going to create a text view inside that linear layout width of zero dp height wrap content  weight one were gonna give it the text of sunday so were gonna have seven of these text views in  our current linear layout were 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 were going to create our recycle view so  ive just typed recycler view were going to give it an id of calendar recycler view width and  height match parent and then im just going to create a new layout resource file im going  to call it calendar cell so this is for our recycler view and inside our calendar cell im  going to create a text view wrap content on the width and height im going to give it an id im  going to call it cell date text fault text to b1 text size is 20 sp and the text color of black  were 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 were going to give it vertical bias  of 0.25 so it just sits a little bit higher than the dead center were going to create a new java  class were 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 were 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 were 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  were going to declare an array list of strings were going to call it days of month and were  just going to create a constructor so just right click and generate a constructor and then inside  our on create view holder were going to declare a layout inflator calling it inflator is equal to  layout inflator from parent get context and then were going to declare a view and were going  to say its equal to inflator.inflate give it our calendar view cell parent and attach to root  is false were going to say view group dot layout params just calling it layout params is equal to  view dot get layout params and then were going to get the height so were going to say layout params  dot get height is equal to just casting to an int parent dot get height times 0.166 were 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 were just going to say holder dot and we havent done this yet so im  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 were just going to return day  of month dot size next what were going to do is implement an on click listener so im just  creating an interface im calling it on item listener void on item click passing in a position  and a string calling it day text and then were going to declare our on-item listener inside our  calendar adapter and add that to the constructor in our calendar view holder were going  to declare a calendar adapter dot on item listener and same thing were going to put this  in the constructor after that were going to say item view set listener were going to pass it  this and we just need to make sure our view holder implements view on click listener and implement  the methods were 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 were finally ready to head into our  main activity java class were going to declare a text view calling it monthly text were going  to declare a recycler view calling it calendar recycle view and were also going to declare  a local date calling it selected date and then inside our on create were just going to create  a method called init widgets and were just going to find both of our views by id so the first one  is calendar recycler view were 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 were going to say selected  date is equal to local date now so just giving it the current date and time and then were going  to say set month view and were going to create this method and were just going to pop it below  our init widgets method were going to say month year text dot set text and were going to create  another method that were going to give our set text were going to call our method month year  from date it receives a local date and were 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 were 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 were going to declare an array list of strings were going to call it days in month and  what were going to do here is actually create another method to get our days in month given our  selected date so were going to declare an array list of strings were going to call it days in  month is equal to a new array list were going to declare a year month object calling it year  month im going to say year month from date im 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 were going to declare another int were  going to call it day of week which is equal to first of month dot get day of week get value so  thats going to return an integer between 0 and 7. were going to create a for loop its going to  be an all-star for loop so were 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 were going to say if i is less than day of week or i is greater than  day of month plus day of week were going to add a blank square if its before the first of the  month or if its 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 were just going to return our days in month array  and then in our set month view were going to declare a calendar adapter were 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 were just going to implement that method but well come back to  that later recycler view layout manager calling it layout manager is equal to new grid layout  manager were just giving the application context and our span count is seven so were going to  have seven columns in our recycle view and then were 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 were just going to say selected date  is equal to selected date minus a month and then set our month view again and were going to do the  same thing in next month but instead were going to plus a month and then set our month view after  that and then in our on item click if our cell doesnt equal nothing so its not a blank cell and  then were just going to make a bit of toast and say youve selected this date so really you could  put whatever you like in here but thats just an example of how you can get the on item click  listener working with your recycler view cool and i didnt like this not being a string resource  ive 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 weve 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

Code With Cal: Calendar App Example Android Studio Tutorial - Mobile App Development