Pop Up Date Picker Android Studio Tutorial

Pop Up Date Picker Android Studio Tutorial


Pop Up Date Picker Android Studio Tutorial

Simple date picker tutorial. Todays example project is how to create a simple pop-up date picker in Android studio using Java.

In this beginner friendly tutorial, we create a button which when tapped displays and Date Picker Dialog which is a pop up with a date picker. Once the user selects a date from the spinner date picker we format the selected date and put the text of our formatted date into the text of our button.

In this tutorial we use a spinner style for our date picker, but the concepts here should work for any date picker styling.

Date Picker Source Code:
https://github.com/codeWithCal/DatePi

⏱️ TIMESTAMPS ⏱️
00:00 - Intro
00:30 - XML Layout
01:52 - Date Picker Java

#DatePicker #AndroidStudio #AppDevelopment


Content

0.32 -> hello and welcome to another java and android  studio tutorial my name is cal and in this  
4.08 -> tutorial we're going to be having a look at date  pickers we're just going to create a very simple  
8 -> pop-up date picker so basically we're going to  have a button when you click on the button it's  
12.4 -> going to show up then alert dialog with the  date picker in it the user then selects the  
16.48 -> date and then we put that date back into the  buttons text really simple shouldn't take too  
21.44 -> long there's a link in the description to the  source code with all that said let's get started
29.76 -> getting started i'm just going to create a  new android studio project empty activity i'm  
33.28 -> going to call this date picker tutorial and our  programming language is java and hit finish to  
38.24 -> create our new android studio project next i'm  going to head into our themes and just delete  
43.68 -> all of the values just because that's a new  android studio thing that they've put in there  
47.76 -> and just keeping this as simple as possible i'm  going to remove all that heading back into the  
52 -> main xml i'm just going to add in an id to our  textview changing the text to date and the text  
58.32 -> size to 30sp the text color is black and i'm going  to remove that top constraint there so that the  
64.32 -> text view aligns at the top and then just say  margin top 30dp and i'm actually going to change  
68.8 -> that to select date it's a bit nice a bit of  text there and then i'm going to add in a button  
72.56 -> i'm going to give it a width of 250dp as well as  height wrap content and then i'm going to give it  
77.04 -> a text of January 1st 2020 text size 30 and color  black as well and then i'm just going to say style  
83.92 -> and then i'm going to give it a question mark and  say android spinner style and then i'm going to  
87.76 -> copy down those two constraints to the left option  to the right of parent just so it centers in the  
94.16 -> view and then i'm also going to give it the  constraint top to bottom of our text view  
99.12 -> as well as margin top 10 dp cool and finally  we're just going to give our button an id  
102.96 -> calling it date pick a button as well as an on  click listener so just saying on click say open  
107.92 -> date picker click on the error message and create  that in the main activity cool and then heading  
112.64 -> up to the top of our main activity we're going  to declare a date picker dialog and i'm just  
116.4 -> naming this date picker dialog as well as a button  just going to call this date button then in our  
121.36 -> own create method we're going to create another  method called init date picker below that method  
125.68 -> we're just going to say date button is equal  to find view by id and then r dot id dot date  
130.48 -> picker button which is the same id that we gave  in the xml and then in our init date pickup method  
134.56 -> we're just going to say date picker dialog dot on  date set listener date set listener is equal to  
139.36 -> new date picker dialog on date set listener  hit enter to implement this override method  
145.36 -> on date set and then we're just going to change  those in so they're actually year month and day  
149.92 -> and then it's got a funny little thing where  the month is actually zero so January is zero  
153.36 -> so we're just going to say month is equal to  month plus one so January is equal to one string  
158.24 -> date is equal to make date string and then create  a new method called make date string which takes  
163.68 -> three ins day month and year and then i'm just  going to return month with a bit of space plus day  
168.4 -> with a little bit of space plus the year and we  could just leave it as is and you get an all-digit  
172.48 -> date format but in my experience in software  development it's worth using this state format  
176.64 -> with the string like so jan feb march because most  people around the world can read and understand  
181.92 -> and are happy with this date format if you start  doing the digit one then you know some countries  
186.08 -> will say oh no you put the month first and some  countries will say you know you put the day first  
189.52 -> and i'm sure there's a country out there that  says no you should put the year first either way  
193.2 -> if you use this date format generally speaking  people are pretty happy and then just returning  
198.08 -> jan as a default the method shouldn't really  ever reach here though so you could probably  
201.52 -> put in whatever you like cool so now we've got  our nicely formatted date string we're just going  
205.6 -> to say datebutton.settext with our date and then  below that i'm just going to say calendar cal is  
211.04 -> equal to calendar dot get instance and i'm going  to say into year is equal to cal dot get calendar  
216.56 -> year and then copy and paste that down twice more  and get month and day so what we're doing here is  
221.68 -> just getting today's date and we're going to use  today's date as our default selected date for our  
226.4 -> date picker next we're going to declare one more  int calling it style and i'm going to say style  
231.12 -> is equal to alert dialog dot theme holo light so  you could pretty much choose whatever style you  
235.52 -> like here but i like to look at this one next date  picker dialog is equal to new date pick a dialogue  
240.64 -> and giving a context of this given then giving  it our style our date set listener year month and  
245.6 -> day now that we've initialized our date picker  dialogue we just need to go into our open date  
249.2 -> picker method and just say datepickerdialog.show  and then finally we're just going to set the date  
254.32 -> button to show today's date by default so i'm just  going to say date button dot set text is equal  
260.32 -> to get today's date so just creating a new method  which returns a string and then i'm going to copy  
264.64 -> what we did for getting today's date from the  calendar and then just giving it month is equal  
269.44 -> to month plus one because of that funny little  month nuance and then set return make date string  
275.28 -> with our day month and year cool and if we build  and run this now we've got a pretty basic pop-up  
280.24 -> date picker so just selecting say December  15th and it's populated that in our date button  
285.6 -> and then let's pick a future date so boxing day  December 26 2020. one other thing i will show you  
291.92 -> if you want to set the maximum date so you can't  pick any dates into the future and date pick a  
296 -> dialogue we're just going to call get date picker  and then set max date giving it system . current  
301.307 -> time and build and run this once more you can see  our popup date picker you can't select any dates  
306.32 -> into the future you could do the same thing for  min date so set min date so say you only wanted  
310.24 -> to pick dates into the future and nothing behind  it's another nice way you can just change set max  
314.56 -> date to set min date cool and that wraps up our  date picker tutorial in android studio using java  
319.28 -> if you like the video give it a like otherwise  i will catch you guys in the next tutorial
338.32 -> you

Source: https://www.youtube.com/watch?v=qCoidM98zNk